Given an array A[1..n] of positive integers and given a number
x, find if
any two numbers in this array sum upto x. That is, are there i,j
such
that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose
now
that A was already sorted, can you obtain O(n) algorithm?
Algorithm in O(nlogn) time for A[i]+A[j] = x:
findpair(A[] ,x)
1. sort Array A by using haep sort . it wiil have O(nlogn) complexity.
2, set low=0
3, set high= arr.size-1
4. while(low <high)
If (A[low] + A[high] == x) then return 1
Else if ( A[low] + A[high] < x ) then low++
Else high++
End IF
end while
5. end function
============================================================================================
This algorithm has complexity of O(mlogn+n) =O(nlogn)
============================================================================================
. Suppose now that A was already sorted, can you obtain O(n) algorithm?
Ans: yes as A is already sorted. There will be only a while loop as shown in the algorithm above. so it will the complexity of On).
Given an array A[1..n] of positive integers and given a number x, find if any two...
Given as input an array A of n positive integers and another positive integer x, describe an O(nlogn)-time algorithm that determines whether or not there exist two elements Ai and AONn the array A such that is exactly x.
Show that any array of integers x[1...n| can be sorted in O(n+M) time, where М = maxxminx; For small M, this is linear time: why doesn't the 2(nlogn) lower bound apply in this case?
Suppose you have a sorted array of positive and negative integers and would like to determine if there exist some value of x such that both x and -x are in the array. Consider the following three algorithms: Algorithm #1: For each element in the array, do a sequential search to see if its negative is also in the array. Algorithm #2:For each element in the array, do a binary search to see if its negative is also in the...
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...
You are given an array of integers, where different integers may have different numbers of digits but the total number of digits over all integers in the array is n. Show how to sort the array in increasing order in O(n) time. Note: The number of integers in the array can be different for same value of n - for example the array with 2 integers 2468 and 12345 has 9 digits as well as the array with 5 integers...
The input is an array of N integers ( sorted ) and an integer X. The algorithm returns true if X is in the array and false if X is not in the array. Describe an algorithm that solves the problem with a worst case of O(log N) time.
We are given a sequence ofndistinct numbers a1,...,an. We define an inversion to be a pair i<j such that ai>aj. Give an O(nlogn) algorithm to count the number of inversions. (Hint: assume that your algorithmreturns a sorted array of input sequences, as well as the number of inversions. Divide the input sequencesinto two halves, then count inversions and sort each half by doing recursive calls, and find a way to mergethese two sorted halves and to count the number of...
We are given an array A holding n integers, for some large n. The array is sorted, and the values in A range from -2147483648 to 2147483647, evenly distributed. Give Θ expressions for the following tasks: A. Running the insertion sort algorithm on the array A: B. Running the selection sort algorithm on the array A: C. Performing binary search for integer k which is not in A: D. Performing interpolation search for integer k not in A:
Hi, I am having trouble with the following question: Given an unsorted array with integers, find the median of it using the Quick Select method we’ve discussed in the class (Hint: We use the quick select to find the kth smallest element in an unsorted array in O(n) time complexity). Note: A median is the middle number of the array after it is sorted. And in this problem, we return the N/2-th number after sorted if there are even numbers...