Question

In: Computer Science

There are two sorted arrays nums1 and nums2 both of size n. Find the median of...

There are two sorted arrays nums1 and nums2 both of size n. Find the median of the two sorted arrays. The overall run time complexity should be O(log(n)).

Solutions

Expert Solution

You can convert this problem to the problem of finding kth element, k is (A's length + B' Length)/2.
If any of the two arrays is empty, then the kth element is the non-empty array's kth element. If k == 0, the kth element is the first element of A or B.

For normal cases(all other cases), we need to move the pointer at the pace of half of the array size to get O(log(n)) time.

public double getSortedArraysMedian(int[] numbers1, int[] numbers2) {
int total = numbers1.length+numbers2.length;
if(total%2==0){
return (getKthElement(numbers1, 0, numbers1.length-1, numbers2, 0, numbers2.length-1, total/2)
+ getKthElement(numbers1, 0, numbers1.length-1, numbers2, 0, numbers2.length-1, total/2-1))/2.0;
}else{
return getKthElement(numbers1,0, numbers1.length-1, numbers2, 0, numbers2.length-1, total/2);
}
}

//k is the index starting from 0
private int getKthElement(int[] numbers1, int i1, int j1, int[] numbers2, int i2, int j2, int k){
if(j1<i1){
return numbers2[i2+k];
}
if(j2<i2){
return numbers1[i1+k];
}

if(k==0){
return Math.min(numbers1[i1], numbers2[i2]);
}

int len1 = j1 - i1 + 1;
int len2 = j2 - i2 + 1;

int m1 = k*len1/(len1+len2);
int m2 = k - m1 - 1;

m1 += i1;
m2 += i2;

if(numbers1[m1]<numbers2[m2]){
k = k-(m1-i1+1);
j2 = m2;
i1 = m1+1;
}else{
k = k-(m2-i2+1);
j1 = m1;
i2 = m2+1;
}

return getKthElement(numbers1, i1, j1, numbers2, i2, j2, k);
}


Related Solutions

Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted)...
Design an O(n log n) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size...
Design an O(nlogn) algorithm that takes two arrays A and B (not necessarily sorted) of size n of real numbers and a value v. The algorithm returns i and j if there exist i and j such that A[i] + B[j] = v and no otherwise. Describe your algorithm in English and give its time analysis.
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
You are given two arrays A1 and A2, each with n elements sorted in increasing order....
You are given two arrays A1 and A2, each with n elements sorted in increasing order. For simplicity, you may assume that the keys are all distinct from each other. Describe an o(log n) time algorithm to find the (n/2) th smallest of the 2n keys assuming that n is even.
Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an...
Suppose you have two sorted arrays of n integers: X0[1..n] and X1[1..n]. Devise and analyze an efficient algorithm for finding the median of all numbers in arrays X0 and X1. Now, suppose you have not two, but three such arrays, each with n elements. Devise and analyze an efficient algorithm for finding the median. Do the same for n arrays, each with n elements.
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
I need to find the kth smallest element in the union of 2 sorted arrays in...
I need to find the kth smallest element in the union of 2 sorted arrays in O(log |A| + log |B|) time. I understand this is similar to binary search, but I don't understand which parts of the arrays I can throw out at each level of recursion, and why. In the pseudocode below, what should go into $1 through $16 and why? // A and B are each sorted into ascending order, and 0 <= k < |A|+|B| //...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n)...
(Linear time algorithm for finding duplicates of two sorted arrays, 20pt) Devise a linear time O(m+n) algorithm to find the duplicates between two sorted arrays (m, n are the sizes of two arrays). For instance, for inputs {1,3,5,7} and {1,2,3,4}, the correct outputs should be {1,3
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT