-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathsol.java
40 lines (33 loc) · 888 Bytes
/
sol.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
public class sol{
public static void main(String[] args) {
// call the fn here
}
}
class Solution {
public double findMedianSortedArrays(int[] m, int[] n) {
int[] merged = new int[m.length + n.length];
int i = 0, j = 0, k = 0;
while (i < m.length && j < n.length) {
if (m[i] <= n[j]) {
merged[k++] = m[i++];
} else {
merged[k++] = n[j++];
}
}
while (i < m.length) {
merged[k++] = m[i++];
}
while (j < n.length) {
merged[k++] = n[j++];
}
int mid = merged.length / 2;
if (merged.length % 2 == 0){
int sum = merged[mid] + merged[mid - 1];
return (double) sum / 2;
}
else{
return merged[mid];
}
// return avg;
}
}