Skip to content

Commit cd3eeb4

Browse files
authored
Merge pull request #31 from thepremshankarsingh/main
Create MedianOfSortedArrays.java
2 parents 0551e6e + caf7a37 commit cd3eeb4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
3+
int n = nums1.length;
4+
int m = nums2.length;
5+
int i=0, j=0;
6+
int idx =0;
7+
double[] arr = new double[n +m];
8+
9+
while(i < n && j < m) {
10+
if(nums1[i] < nums2[j]) {
11+
arr[idx++] = nums1[i++];
12+
}
13+
else {
14+
arr[idx++] = nums2[j++];
15+
}
16+
}
17+
while(i < n) {
18+
arr[idx++] = nums1[i++];
19+
}
20+
while(j < m) {
21+
arr[idx++] = nums2[j++];
22+
}
23+
int len = arr.length;
24+
int mid = len/2;
25+
return len%2 == 1 ? arr[mid] : (arr[mid] + arr[mid -1]) /2;
26+
}
27+
}
28+
29+
30+
31+
32+
33+

0 commit comments

Comments
 (0)