retagged by
4,755 views
3 3 votes
Given 2 sorted arrays each of n-elements and distinct. How much time it will take to find middle element of union array?

(a) O(1)

(b) O(log n)

(c) O(n)

(d) None of these

5 Answers

9 9 votes
1 1 vote
Answer would be O(nlgn)

In order to find middle of union array atleast half of both the arrays should be compared and we can do this by using binary search on both the arrays, so three cases arrives,

1) both m/2 and n/2 array are covered then we will find the middle element (or)

2) array 1 is totally over (or)

3) array 2nd is totally over

 

In all these three cases binary search will take O(nlgn)
1 1 vote
@arjun , @bala sir can you please check ...i think answer should be O(n) as it will take O(n+n) time (merge procedure) to create 1 sorted array and then O(1 ) to get middle element.....

and O(log n) is not right? and if not why?
0 0 votes
It will take O(n)  because you have to first find the union of both both array is sorted than we can find union by merge algorithm and you know merge algorithm take O(n) time after that middle element n/2 constant time O(1) overall it will take O(n) time
0 0 votes

Edit 1: This question is a duplicate of https://gateoverflow.in/227458/sorting?fbclid=IwAR1GJNWosv560XE1VFqji7KAwgPoVjLXgZuKF0CLfMPDGDR2J6PsTYYjPcs 

Edit 2: For getting the median/middle efficiently we don't have to necessarily perform the union or merge operation. The method 2 shown here https://www.geeksforgeeks.org/median-of-two-sorted-arrays/ works better than finding the union first and finding the median. So it can be done in O(log n ) time.

 

 

 

 

Why not O(n)? 

The question is asking for the middle element of the union of 2 sorted arrays. By "middle" I am assuming that it wants the value at the middle index, please correct me if I am wrong here. I did not see anything in the question that makes me search for the median as others have suggested. 

Consider 

A[] = {1,3,4,5,7}
B[] = {2,3,5,6,8}

Both the sorted arrays are specified as given. Both have equal length and distinct elements in each. 

Now the Union array would be 

U[] = {1,2,3,4,5,6,7,8}

Which would take O(n+n) time. 

Now, middle of U[] can be found out by

int mid = firstIndex + (lastIndex-firstIndex)/2

which would take O(1) time, and so total time would be O(n).  

edited by
Position:
Show:

Related questions

0 0 votes
2 answers 2 answers
2.7k
2.7k views
Rustam Ali asked Sep 5, 2018
2,695 views
What is the time complexity of calculating power of an element using DAC?
1 1 vote
1 answers 1 answer
1.0k
1.0k views
Emankashyap asked Apr 30, 2024
1,047 views
In quick sort, n numbers the (n/10)th element is selected as pivot using n^2 sortimng time complexity what will be the time complexity of quick sort is.....a)O(nlogn)b)O(...
1 1 vote
1 1 answer
796
796 views
meethunjadhav asked Jul 30, 2018
796 views
suppose merge sort takes 2 sec to sort a set of 64 keys then how much time will take to sort a set of 512 keys?here, ans is 24 sec how it is plz explain me.
1 1 vote
1 1 answer
919
919 views
chetan raghav asked Sep 3, 2017
919 views
Given an unsorted array. The array has this property that every element in array is at most k distance from its position in sorted array where k is a positive integer sma...