GeeksforGeeks » Algorithms
kth smallest element in the union of the arrays in a logarithmic time algorithm
(9 posts)-
Given two *sorted* arrays of n elements each, is it possible to find the kth smallest element in the union of the arrays in a logarithmic time algorithm?
-
Let us first take the k/2th element of both the arrays(say array S1 and S2). If k/2th element of array S1 is greater than the k/2th element of S2 but smaller than (k/2 + 1) element of S2, then k/2th element of array S1 is the answer. (Vice versa is also true)
If k/2th element of S1 is greater than both k/2 and (k/2 + 1) element of S2, then consider 3k/4th (k/2 + k/4) element of S2 and k/4th (k/2-k/4) element of S1 and compare them in the fashion described above(If the element under consideration of one array is greater than the element under consideration of another array but less than the +1 element, then it is the answer).
Each time, increase the pointer of one array by k/(2^i) and decrease the pointer of the other array by k/(2^i) (i is the number of iteration) till the above condition is satisfied.
This way you can find kth element in O(log n) time
-
To give an example:
Array S1: 5, 7, 8, 9, 12
Array S2: 3, 10, 11, 14, 15We need 4th smallest element.
First iteration. 4/2 which is 2nd element of both arrays are considered.
7 < 10 and 8 < 10.So increase the pointer of S1 by 4/4 ie 3rd position and decrease the pointer of S2 by 4/4 ie 1.
8 > 3 and 8 < 10 and hence 8 is the answer in this case. -
You can modify the 3 approaches for median of two sorted arrays (see http://geeksforgeeks.org/?p=2105) to get the kth largest element. You just need to change the conditions where you look for median. Here, you need to look for (2n-k+1)kth element if arrays are sorted in ascending order and have total 2n elements, if sorted in descending order then look for kth.
-
it is not true for all the cases please modify it
-
int findKthSmallest(int a[], int n, int b[], int m, int k) { if(n == 1 || m == 1) { if(n == 1) { if(m > k) { if (b[k] < a[0]) return b[k]; else if (k != 0 && b[k-1] > a[0]) return b[k-1]; else return a[0]; } else if(b[m-1] > a[0]) return b[m-1]; else return a[0]; } else { if(n > k) { if (a[k] < b[0]) return a[k]; else if (k != 0 && a[k-1] > b[0]) return a[k-1]; else return b[0]; } else if(a[n-1] > b[0]) return a[n-1]; else return b[0]; } } if(a[n-1] < b[0]) { if(k < n) return a[k]; else return b[k - n]; } else if(b[m-1] < a[0]) { if(k < m) return b[k]; else return a[k - m]; } int a1 = (n / 2); int b1 = (m / 2); if(a[a1] < b[b1]) { if(k < a1 + b1 + 1) return findKthSmallest(a, n, b, m - b1, k); return findKthSmallest(a + a1, n - a1, b, m ,k - a1); } else { if(k < a1 + b1 + 1) return findKthSmallest(b, m, a, n - a1, k); return findKthSmallest(b + b1, m - b1, a, n ,k - b1); } } -
To Re-iterate what was said earlier in a more elaborate way:
Lets consider 2 arrays A, B both with n elements.
Now k the smallest element of both the Arrays put to gather cannot line in A[k] to A[N] or B[k] to B[N] (as both arrays are sorted)Now we can reuse the logic to find the median of 2 sorted arrays in logn time: except that we set boundary of the arrays to be smaller. A[0... k -1], B[0...k-1]. Since there are 2k elements in this, the median of this portion of both the arrays will indeed be the kth smallest element of both the arrays put together.
-
I found this explanation helpful http://aleph.nu/blog/kth-smallest-in-sorted-union.html
Reply
You must log in to post.