Skip to content

Commit 491ca9b

Browse files
committed
Added median of two sorted arrays solution in leetcode folder
1 parent ea4904d commit 491ca9b

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/**
2+
* Question Link : https://leetcode.com/problems/median-of-two-sorted-arrays/
3+
*
4+
* Problem Statement : Given two sorted arrays nums1 and nums2 of size m and n respectively,
5+
* return the median of the two sorted arrays.
6+
* The overall run time complexity should be O(log (m+n)).
7+
*
8+
* Intuition : Both arrays are sorted and expected time complexity is logarithmic, we can try using Binary Search
9+
*
10+
* Example :
11+
* nums1 = [1, 3, 4, 7, 10, 12]
12+
* nums2 = [2, 3, 6, 15]
13+
*
14+
* Observation :
15+
* In Combined array :
16+
* [1, 2, 3 , 3, 4, 6, 7, 10, 12 ,15]
17+
* we need two middle elements(in case where m+n is even) or single middle element(in case where m+n is odd)
18+
*
19+
* |
20+
* [1, 2, 3 , 3, 4,| 6, 7, 10, 12 ,15]
21+
* |
22+
*
23+
* If we partition the array in two halves, we need largest element from first half and smallest element from second half
24+
*
25+
* From two arrays, partition can be created in following ways
26+
*
27+
*
28+
* nums1 : [1,3,4,7,10] | [12]
29+
* |
30+
* nums2 : | [2,3,6,15]
31+
* |
32+
* ______________________________________________
33+
*
34+
* nums1 : [1,3,4,7] | [10,12]
35+
* |
36+
* nums2 : [2] | [3,6,15]
37+
* |
38+
* _______________________________________________
39+
*
40+
* nums1 : [1,3,4] | [7,10,12]
41+
* |
42+
* nums2 : [2,3] | [6,15]
43+
* |
44+
* _______________________________________________
45+
*
46+
* nums1 : [1,3] | [4,7,10,12]
47+
* |
48+
* nums2 : [2,3,6] | [15]
49+
* |
50+
* ______________________________________________
51+
*
52+
* nums1 : [1] | [3,4,7,10,12]
53+
* |
54+
* nums2 : [2,3,6,15] |
55+
* |
56+
* ______________________________________________
57+
*
58+
* If the largest element of nums1[] in left partition
59+
* is less than or equal to smallest element of nums2[] in right patrtition
60+
* and
61+
* If the largest element of nums2[] in left partition
62+
* is less than or equal to smallest element of nums1[] in right patrtition
63+
*
64+
* Then that partion is a valid partition
65+
*
66+
*/
67+
68+
class Solution {
69+
public double findMedianSortedArrays(int[] nums1, int[] nums2)
70+
{
71+
if(nums2.length < nums1.length)
72+
{
73+
return findMedianSortedArrays(nums2, nums1);
74+
}
75+
int n = nums1.length;
76+
int m = nums2.length;
77+
78+
int low = 0;
79+
int high = n;
80+
81+
while(low<=high)
82+
{
83+
int partition1 = (low+high)>>1;
84+
int partition2 = (n+m+1)/2 - partition1;
85+
86+
int left1 = (partition1==0)?Integer.MIN_VALUE : nums1[partition1-1];
87+
int left2 = (partition2==0)?Integer.MIN_VALUE : nums2[partition2-1];
88+
89+
int right1 = (partition1==n)?Integer.MAX_VALUE : nums1[partition1];
90+
int right2 = (partition2==m)?Integer.MAX_VALUE : nums2[partition2];
91+
92+
if(left1<=right2 && left2<=right1)
93+
{
94+
if((n+m)%2==0)
95+
{
96+
return (max(left1, left2)+min(right1,right2))/2.0;
97+
}
98+
else
99+
{
100+
return max(left1, left2);
101+
}
102+
}
103+
else if(left1 > right2)
104+
{
105+
high = partition1-1;
106+
}
107+
else
108+
{
109+
low = partition1+1;
110+
}
111+
}
112+
113+
return 0.0;
114+
115+
}
116+
117+
public int max(int a, int b)
118+
{
119+
return a>b? a:b;
120+
}
121+
public int min(int a, int b)
122+
{
123+
return a<b? a:b;
124+
}
125+
}
126+
127+
128+
129+
130+
131+
132+
133+
134+
135+
136+
137+

0 commit comments

Comments
 (0)