In this exercise we are given a sequence (array) A = {a;}"-1 such that a; = 0 or a; = 1, Vi e {1,.., n}. Suggest an iterative algorithm that shuffles all elements of A and returns an index k e {1,...,n} such that a; = 0 for all i < k, and a; = 1 for all j > k. One condition for your solution is that it should not use any additional array B onto which to copy temporary elements (no credits will be given to a solution that makes use of such auxiliary storage).
Q: Describe an efficient algorithm that, given a set {x1,x2,.......xn} of points on the real line,…
A: Answer: The array of n points on the real line is A[1..n]: Points A[1] < A[2] < … < A[n]…
Q: Question1 (b)Consider the followingalgorithmwhere Small is an array of ninteger values:…
A: The question is to find the actual working of the code given in Algorithm Q1.
Q: prove that the following algorithm finds the minimum integer in an array A of n integers (write the…
A: To prove that this algorithm finds the minimum integer in the array, it's the correctness of its…
Q: A magic square of order n is an arrangement of the integers from 1 to n2 in an n × n matrix, with…
A: Actually, the answer has given below:
Q: You are given an array of positive integers S[1, . . . , n]. You are initially positioned at the…
A: Naive approach: Start from the first element and recursively call for all the elements reachable…
Q: Given a sorted array A of n distinct integers, some of which may be negative, give an O(log(n))…
A: Given: Given a sorted array A of n distinct integers, some of which may be negative, give an…
Q: What is the time complexity of the given algorithm below? An algorithm, M, for sorting a given…
A: n is the number of elements in the array A We are always comparing marking the first element but the…
Q: (b). write an algorithm for Given a set of distinct, positive integers x1, x2, x3, ...xn, where n is…
A: (b). write an algorithm for Given a set of distinct, positive integers xl, x2, x3, ...xn, where n is…
Q: Let T be a sorted array of n elements. An element x is said to be a majority element in Tif the…
A: here we have uses Boyer–Moore majority vote algorithm Algorithm : Initialize an majority element m…
Q: Consider the following algorithm: Input: Array A and B both with n integers. Output: Array C with n…
A: According to the algorithm mention we have to find out the tight upper bound on the running time of…
Q: On his way back from detention, Harry runs into his friend Hermione. He is upset because Professor…
A: Solution:- We already know that sorting the array requires omega(n log n) comparisons. If k >…
Q: Question 2: Recurrences (a) Below is the pseudo-code for two algorithms: Practice1(A,s,f) and…
A: Given The answer is given below For Algorithm 1: From the above pseudo code we have The worst case…
Q: Let A be an array of n integers, where n is divisible by 4. Describe an algorithm with O(n) time…
A: Here I've similar question which might help you in this question : Given a variety of size n,…
Q: This is a memoized version which uses a global array Q to store the results of computations.…
A: The algorithm given is:- for (i=0; i< length[Q]; i++){ Q[i] = -1; } int Q-Optimal(int n)…
Q: Write an algorithm to find the first two largest numbers in an in array of integers
A: Induction Hypothesis: Define the rule we want to prove for every n, let's call the rule F(n)…
Q: Implement an algorithm that returns the X closest items to a value V, for any array of integer…
A: Merge sort is the one of the algorithm used to divide and conquer strategy. MergeSort(A, p, r):…
Q: Assume n is power of 2 and let a be a given real number. Let A, be a n X n matrix aAm/2 ). Give An/2…
A: Since, both the matrix are of n x n size Count the total number of scalar operations that are…
Q: Suppose you are given a very long array A, whose first n elements are integers (in arbitrary order)…
A: Here have to determine algorithm for find n in logn time.
Q: Given an n×n matrix M in which every entry is either a 0 or 1. Present an algorithm that determines…
A: Every cell contains a whole number from 1 and n (comprehensive).No whole number shows up two times…
Q: Given an array A that contains a set of positive integer values n, where 4≤ n ≤ 100. Now, consider…
A: According to the information given:- We have to write a code on the basic of instruction mentioned.
Q: Write an efficient algorithm for the following problem, and describe your reasoning. Determine the…
A: First we will count the number of zeros in whole array, which will take time complexity of O(n). If…
Q: Some problem of size n can be solved recursively by dividing it into 4 subproblems, where the size…
A: According to the given question a problem of size n is divided into 4 sub-problems and size of each…
Q: Consider the array A[1 . . . n] consisting of n non-negative integers. There is a frog on the LAST…
A: There exists a greedy solution for this problem: This solution does not use the nested loop it…
Q: Let A be an array with n = 2k − 1 elements, where k is some positive integer. Determine a formula…
A: The quick sorting algorithm is highly effective and is based on the division of the data array into…
Q: h that at most k elements of A are less than or equal to x, or nil if no such element exists. A…
A: Code: #include<iostream> using namespace std; int size=15;int…
Q: Find an 8-element array that require most number of Comparisons. Let`s assume S(n) = [?/2 + 1, 1,…
A: Shell sort algorithm works with most number of comparisons in sorting. In shell sort, elements at a…
Q: Given an array A of n positive numbers. The task is to find the first Equilibium Point in the array.…
A: Ans : C++ code: #include <bits/stdc++.h>using namespace std; int equilibrium(int arr[], int…
Q: Consider the following pseudo-code: function meh(array a[0 - - - n – 1]) 40 if n > 2 then for i + O…
A: the time complexity of the above pseudo-code is O(n2). the fourth option is correct checkout…
Q: Modify the Partition function so that after running it, any input array A is partitioned into three…
A: Pivot value divides the list into two parts. And recursively, we find the pivot for each sub lists…
Q: Problem 2: Search an Array Vn). We want Considering the search problem, we have a list of n integers…
A: Pseudocode: LinearSearch(A, ν): for i = 0 to A.size()-1: if A[i] == ν return i…
Q: Suppose we have an array A 1....n of even number of elements. Suppose we pair up (A[1], A[2]),…
A: Solution : The given array A[1....n] of even number of elements. After recall the majority function…
Q: In IR2 , we define a slab to be a pair of parallel lines. Given a set of points P in IR2 , find the…
A: The brute force algorithm doesn't apply any advanced technique, it is a very straightforward…
Q: Consider the following algorithm, which takes as input a sequence of n integers a_1, a_2,… , a_n and…
A: (a) As you can see, to compute the matrix M we need min(mij,mk), for this we need 3 for loops of i,…
Q: Consider the following algorithm Algo-X that takes an array A of n numbers and explain working of…
A: An algorithm is a step-by-step process of solving a problem or task. For example, add two numbers a…
Q: An array A contains n integers taken from the interval [0,4n ), with repetitions allowed. Describe…
A: Pseudo-code:-
Q: the proof by showing that T(n) <= an +b, for some constants a and b, and derive the values of a and…
A: the proof by showing that T(n) <= an +b, for some constants a and b, and derive the values of a…
Q: Question 2: Recurrences (a) Below is the pseudo-code for two algorithms: Practicel(A,s,f) and…
A: For algorithm 1 From the above pseudo code we have The worst case time complexity will be (…
Q: The ColumnChoice problem takes as input a two-dimensional array A[1..m, 1..n] of Os and 1s with m…
A: A[2][10] location will be at 370
Q: Suppose we have three points in a two dimensional space: (3,1), (2,1), and (7,7). We want to perform…
A:
Q: Considering the search problem, we have a list of n integers A = (v1, v2, · · · Un). We want to…
A: Ans:) In this problem the implementation can be done using two ways: If the array is in increasing…
Q: We have an array A[1 : n] consisting of n positive integers in {1,..., M}. Our goal is to check…
A: Given: An array A[1 : n] consisting of n positive integers in {1, . . ., M}. Asked: to find the…
Q: Consider a simplified version of bucket-sort. You are given an array A of n random integers in the…
A: the answer is in the 2nd steps
Q: Given an N-by-N matrix of integer numbers, find the largest number that appears (at least) once in…
A: Introduction :Given ,We have find the time complexity of the best case algorithm.
Q: Problem 3. Given an array A[1 : n] of n positive integers (possibly with repetitions), your goal is…
A: Below is the complete solution with Program Images and Output Images. Note:- In the given question,…
Q: An array of n elements is almost sorted if and only if every element is at most k spots away from…
A: Actually, array is a collection of elements.
Q: Subject: Data Structures Given an N-by-N matrix of integer numbers, find the largest number that…
A: Algorithm : def GetCommonLargest(D[N][N]): C = Get Unique Elements of Matrix in set C M = Create…
Step by step
Solved in 2 steps with 3 images
- Given two arrays a and b, where both arrays consisting of n positive integers. It is allowed to swap any element from array a with any element from b. Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k moves (swaps). Input • The first line of the test case contains two integers n and k (1Sns30;0sksn) - where n is the number of elements in a and b, while k is the maximum number of moves you can do. • The second line of the test case contains n integers a1,a2,... an • The third line of the test case contains n integers b1,b2,..,bn Output Print the answer – the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps. Examples: Ex 1: Ex 2: Ex 3: Input 55 53 40 55665 1234 5 2243 12543 10 9 10 10 9 2423 Output: 27 39 11 • In the first test case of the example, you don't need to swap anything. • In the second test case of the example, you can swap a1=1 with bi=10, a3=3 with b3=10…Consider an n by n matrix, where each of the n2 entries is a positive integer. If the entries in this matrix are unsorted, then determining whether a target number t appears in the matrix can only be done by searching through each of the n2 entries. Thus, any search algorithm has a running time of O(n²). However, suppose you know that this n by n matrix satisfies the following properties: • Integers in each row increase from left to right. • Integers in each column increase from top to bottom. An example of such a matrix is presented below, for n=5. 4 7 11 15 2 5 8 12 19 3 6 9 16 22 10 13 14 17 24 1 18 21 23 | 26 | 30 Here is a bold claim: if the n by n matrix satisfies these two properties, then there exists an O(n) algorithm to determine whether a target number t appears in this matrix. Determine whether this statement is TRUE or FALSE. If the statement is TRUE, describe your algorithm and explain why your algorithm runs in O(n) time. If the statement is FALSE, clearly explain why no…Given two arrays a and b, where both arrays consisting of n positive integers. It is allowed to swap any element from array a with any element from b. Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (Le. at most) k moves (swaps). Input • The first line of the test case contains two integers n and k (1Given two arrays a and b, where both arrays consisting of n positive integers. It is allowed to swap any element from array a with any element from b. Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k moves (swaps). Input • The first line of the test case contains two integers n and k (1sns30;0sksn) - where n is the number of elements in a and b, while k is the maximum number of moves you can do. • The second line of the test case contains n integers a1,a2,... an • The third line of the test case contains n integers b1,b2,..,bn Output Print the answer – the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps. Examples: Ex 1: Ex 2: Ex 3: Input 55 53 40 55665 12345 2243 12543 10 9 10 10 9 2423 Output: 27 39 11 • In the first test case of the example, you don't need to swap anything. • In the second test case of the example, you can swap a1=1 with b1=10, a3=3 with b3=10…Question A: Mapping. The two-sum problem is a popular algorithm problem. Given an array of integers and an integer target, return indices of the two numbers such that they add up to the target. You may assume that each input would have exactly one solution, and you may not use the same element twice. For example, suppose we have an array arr = [1,10,100]. If the target is 11, you should return [0,1] because arr[0] + arr[1] = 1+ 10 = 11. If the target is 101, you should return [0,2] because arr[0] + arr[2] = 1+ 100 = 101. Complete this problem in O(n). Hint: using a hash table.This is exercise 2 We are now also given an array of integers A = {a;}"=1, and some random integer k. 1. Develop an algorithm that returns an array B of all pairs of indices (i, j), such that i and j belong to {1, ...,n}, and (a; + a;)/2 = k. 2. Obtain the running time of the suggested algorithm. 3. Prove correctness of this algorithm by stating and proving a loop invariant. Exercise 3 Suppose we wish to revisit exercise 2 above, and we are given that the list A is sorted in increasing order. 1. Develop an algorithm that solves the same problem as exercise 2 above, but uses Binary search to reduce the number of comparisons needed. 2. Verify that the run-time of the proposed algorithm is indeed faster. How much is it, and why is it faster?... Array A contains positive numbers that can be smaller than 1, larger than 1, or equal to 1. Find the sub-array A[..], such that the product of numbers in A[i..j] (i.e., A[i] × A[i+1] x x A[j - 1] x A[j]) is maximized. Of course, you should describe the fastest algorithm that you can find. A product of two numbers can be computed in time 0(1). Describe your algorithm in words, provide a pseudocode and briefly explain why your algo- rithm is correct.Let A and B be two sorted arrays, each with n elements. For the purposes of this problem, assume that all 2n elements are distinct integers, and assumen= 2k + 1 for some integer k ≥ 1. In this problem, we wish to compute themedianof the 2nelements, which will be the average of the nth and (n+ 1)th smallest elements in the (combined) array. For example, suppose A= [1,2,4,8,16,32,64,128,256] and B= [30,40,50,60,70,80,90,100,110]. If we merge the arrays and sort it, we get C= [1,2,4,8,16,30,32,40,50,60,64,70,80,90,100,110,128,256]. From this we see that the middle two elements are 50 and 60 and so the median is 55. Question: Consider the following algorithm: merge the two arrays and sort it to produce a new array C with 2n elements. Then the median must be (C[n] +C[n+ 1])/2. Determine whether this algorithm is linear or linearithmic. Clearly justify your answer.In searching an element in an array, linear search can be used, even though simple to implement, but not efficient, with only O(n) time complexity. Assuming the array is already in sorted order, modify the search function below, using a better algorithm, so the average time complexity for the search function is O(log n). include <iostream> using namespace std; int search(int al), int s, int v) { 1/ Modify below codes. for (int i = 0; i <s; i++) { if (a[i] = v) return i; return -1; int main() { int intArray:10] = { 5, 7, 8, 9, 10, 12, 13, 15, 20, 34); // Search for element '12' in 10-elements integer array. cout << search(intArray, 10, 12); // '5' will be printed out. // Search for element '35' in 10-elements integer array. cout << search(intArray, 10, 35); // '-1' will be printed out. // Index '-l' means that the element is not found. return 0;You are given an arrayof colors (reds, whites, and blues), in no particular order. Sort them into the order of redsfollowed by whites followed by blues.a. Devise an efficient algorithm to solve this problem.b. What is your algorithm's time complexity?For example, if red is represented by 2, white represented by 4 and blue is represented by 6,your input and output array could be of following form:Input: [ 4 6 2 4 2 6 4 2 6 4 4 2 4 ]Output: [ 2 2 2 2 4 4 4 4 4 4 6 6 6]You have been given an array A of size N and an integer K. This array consists of N integers ranging from 1 to 10^7. Each element in this array is said to have a Special Weight. The special weight of an element a[i] is a[i]%K. You now need to sort this array in Non-Increasing order of the weight of each element, i.e the element with the highest weight should appear first, then the element with the second highest weight and so on. In case two elements have the same weight, the one with the lower value should appear in the output first. Input Format: The first line consists of two space separated integers N and K. The next line consists of N space separated integers denoting the elements of array A. Output Format: Print N space separated integers denoting the elements of the array in the order in which they are required. Constraints: 1≤N≤10^5 1≤A[i]≤10^7 1≤K≤10^7 Note: You need to print the value of each element and not their weight. SAMPLE INPUT 5 21 2 3 4 5 SAMPLE OUTPUT 1 3 5 2 4…The ColumnChoice problem takes as input a two-dimensional array A[1..m, 1..n] of Os and 1s with m rows and n columns along with a non-negative integer k < n. It asks whether there exists a subset SC {1, ..., n} of k columns such that for each row i e {1,..., m} there exists at least one column j e S where A[i, j] = 1. Prove that ColumnChoice is NP- complete.SEE MORE QUESTIONS