Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 259077b

Browse files
author
Tushar Roy
committed
Update median of two sorted arrays
1 parent 02cca5e commit 259077b

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

‎src/com/interview/binarysearch/MedianOfTwoSortedArrayOfDifferentLength.java‎

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,65 @@
55
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
66
*
77
* Solution
8-
* Take minimum size of two array. Possible number of cuts are from 0 to m in m size array.
8+
* Take minimum size of two array. Possible number of partitions are from 0 to m in m size array.
99
* Try every cut in binary search way. When you cut first array at i then you cut second array at (m + n + 1)/2 - i
10-
* Now try to find the i where a[i-1] <= b[j] and b[j-1] <= a[i]. So this i is the answerr
10+
* Now try to find the i where a[i-1] <= b[j] and b[j-1] <= a[i]. So this i is partition around which lies the median.
1111
*
12-
* Time complexity is O(log(min(m,n))
12+
* Time complexity is O(log(min(x,y))
13+
* Space complexity is O(1)
1314
*
1415
* https://leetcode.com/problems/median-of-two-sorted-arrays/
1516
* https://discuss.leetcode.com/topic/4996/share-my-o-log-min-m-n-solution-with-explanation/4
1617
*/
1718
public class MedianOfTwoSortedArrayOfDifferentLength {
19+
1820
public double findMedianSortedArrays(int input1[], int input2[]) {
21+
//if input1 length is greater than switch them so that input1 is smaller than input2.
1922
if (input1.length > input2.length) {
2023
return findMedianSortedArrays(input2, input1);
2124
}
22-
if (input1.length == 0) {
23-
return getMedian(input2);
24-
}
25-
int m = input1.length;
26-
int n = input2.length;
25+
int x = input1.length;
26+
int y = input2.length;
2727

2828
int low = 0;
29-
int high = m;
29+
int high = x;
3030
while (low <= high) {
31-
int cut1 = (low + high)/2;
32-
int cut2 = (m + n + 1)/2 - cut1;
31+
int partitionX = (low + high)/2;
32+
int partitionY = (x + y + 1)/2 - partitionX;
3333

34-
int a1 = (cut1 == 0) ? Integer.MIN_VALUE : input1[cut1 - 1];
35-
int a2 = (cut1 == m) ? Integer.MAX_VALUE : input1[cut1];
34+
//if partitionX is 0 it means nothing is there on left side. Use -INF for maxLeftX
35+
//if partitionX is length of input then there is nothing on right side. Use +INF for minRightX
36+
int maxLeftX = (partitionX == 0) ? Integer.MIN_VALUE : input1[partitionX - 1];
37+
int minRightX = (partitionX == x) ? Integer.MAX_VALUE : input1[partitionX];
3638

37-
int b1 = (cut2 == 0) ? Integer.MIN_VALUE : input2[cut2 - 1];
38-
int b2 = (cut2 == n) ? Integer.MAX_VALUE : input2[cut2];
39+
int maxLeftY = (partitionY == 0) ? Integer.MIN_VALUE : input2[partitionY - 1];
40+
int minRightY = (partitionY == y) ? Integer.MAX_VALUE : input2[partitionY];
3941

40-
if (a1 <= b2 && b1 <= a2) {
41-
if ((m + n) % 2 == 0) {
42-
return ((double)Math.max(a1, b1) + Math.min(a2, b2))/2;
42+
if (maxLeftX <= minRightY && maxLeftY <= minRightX) {
43+
//We have partitioned array at correct place
44+
// Now get max of left elements and min of right elements to get the median in case of even length combined array size
45+
// or get max of left for odd length combined array size.
46+
if ((x + y) % 2 == 0) {
47+
return ((double)Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY))/2;
4348
} else {
44-
return (double)Math.max(a1, b1);
49+
return (double)Math.max(maxLeftX, maxLeftY);
4550
}
46-
} else if (a1 > b2) {
47-
high = cut1 - 1;
48-
} else {
49-
low = cut1 + 1;
51+
} else if (maxLeftX > minRightY) {//we are too far on right side for partitionX. Go on left side.
52+
high = partitionX - 1;
53+
} else {//we are too far on left side for partitionX. Go on right side.
54+
low = partitionX + 1;
5055
}
5156
}
57+
58+
//Only we we can come here is if input arrays were not sorted. Throw in that scenario.
5259
throw new IllegalArgumentException();
5360
}
5461

55-
privatedoublegetMedian(intarr[]){
56-
if(arr.length % 2 == 0){
57-
return ((double)arr[arr.length/2 - 1] + arr[arr.length/2])/2;
58-
} else {
59-
returnarr[arr.length/2];
60-
}
62+
publicstaticvoidmain(String[] args) {
63+
int[] x = {1, 3, 8, 9, 15};
64+
int[] y = {7, 11, 19, 21, 18, 25};
65+
66+
MedianOfTwoSortedArrayOfDifferentLengthmm = newMedianOfTwoSortedArrayOfDifferentLength();
67+
mm.findMedianSortedArrays(x, y);
6168
}
6269
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /