|
| 1 | +/* |
| 2 | +Question: Find Kth smallest element in two sorted arrays |
| 3 | +Source: http://algorithmsandme.blogspot.in/2014/12/fins-kth-smallest-element-in-two-sorted.html#.VLNiz5_0_VN |
| 4 | +*/ |
| 5 | + |
| 6 | +package FindKthSmallestElementBetweenTwoSortedArraysOfUnequalLength; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Scanner; |
| 10 | + |
| 11 | +public class UsingRecursion { |
| 12 | +public static void main(String[] args) { |
| 13 | + Scanner in = new Scanner(System.in); |
| 14 | + try{ |
| 15 | + System.out.println("Enter the number of elements in the first SORTED array"); |
| 16 | + int n = in.nextInt(); |
| 17 | + int[] array1 = new int[n]; |
| 18 | + System.out.println("Enter the elements of the first SORTED array"); |
| 19 | + for(int i=0;i<n;i++) |
| 20 | + array1[i]=in.nextInt(); |
| 21 | + System.out.println("Enter the number of elements in the second SORTED array"); |
| 22 | + int m = in.nextInt(); |
| 23 | + int[] array2 = new int[m]; |
| 24 | + System.out.println("Enter the elements of the second SORTED array"); |
| 25 | + for(int i=0;i<m;i++) |
| 26 | + array2[i]=in.nextInt(); |
| 27 | + System.out.println("Enter the kth smallest element to be found"); |
| 28 | + int k = in.nextInt(); |
| 29 | + System.out.println("The kth smallest element of the two SORTED arrays is: "+findKthSmallestElement(array1,array2,array1.length,array2.length,k)); |
| 30 | + } |
| 31 | + finally{ |
| 32 | + in.close(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +private static int findKthSmallestElement(int[] a, int[] b, |
| 37 | + int aLength, int bLength, int k) { // All the 5 parameters passed are VERY VERY IMP |
| 38 | + |
| 39 | + /* to maintain uniformity, we will assume that size_a is smaller than size_b |
| 40 | + else we will swap array in call :) */ |
| 41 | + if(aLength>bLength) |
| 42 | + return findKthSmallestElement(b, a, bLength, aLength, k); |
| 43 | + |
| 44 | + /* We have TWO BASE CASES |
| 45 | + * Now case when size of smaller array is 0 i.e there is no elemt in one array*/ |
| 46 | + //BASE CASE 1. If the smallest array length is 0 |
| 47 | + if(aLength == 0 && bLength > 0) |
| 48 | + return b[k-1]; // due to zero based index |
| 49 | + |
| 50 | + /* case where k==1 that means we have hit limit */ |
| 51 | + //BASE CASE 2. If k==1 |
| 52 | + if(k==1) |
| 53 | + return Math.min(a[0], b[0]); |
| 54 | + |
| 55 | + /* Now the divide and conquer part */ |
| 56 | + int i = Math.min(aLength, k/2) ; // k should be less than the size of array |
| 57 | + int j = Math.min(bLength, k/2) ; // k should be less than the size of array |
| 58 | + |
| 59 | + if(a[i-1] > b[j-1]) |
| 60 | + // Now we need to find only K-j th element |
| 61 | + return findKthSmallestElement(a, Arrays.copyOfRange(b, j, b.length), a.length, b.length -j, k-j); |
| 62 | + else |
| 63 | + return findKthSmallestElement(Arrays.copyOfRange(a, i, a.length), b, a.length-i, b.length, k-i); |
| 64 | +} |
| 65 | +} |
| 66 | +/* |
| 67 | +Analysis: |
| 68 | + Time Complexity = O(log(n+m)) |
| 69 | + Space Complexity = O(1)*/ |
0 commit comments