forked from iRupam/NewtonSchoolInfinityJune21
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMedianOfTwoSortedArrays2.java
More file actions
64 lines (55 loc) · 1.89 KB
/
MedianOfTwoSortedArrays2.java
File metadata and controls
64 lines (55 loc) · 1.89 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package InfinityJune21.DivideAndConquer;
public class MedianOfTwoSortedArrays2 {
static int median(int arr[], int start, int end) {
int size = end - start + 1;
if(size % 2 == 0) {
return (arr[start + (size / 2)] + arr[start + (size / 2 - 1)]) / 2;
}
else {
return arr[start + (size / 2)];
}
}
static int getMedian(int[] a, int[] b,
int startA, int startB, int endA, int endB) {
if(endA - startA == 1) {
return (Math.max(a[startA], b[startB]) +
Math.min(a[endA], b[endB])) / 2;
}
//Median of the first array
int m1 = median(a, startA, endA);
//Median of the second array
int m2 = median(b, startB, endB);
if(m1 == m2) {
return m1;
}
else if(m1 < m2) {
return getMedian(a, b, (endA + startA + 1) / 2,
startB, endA, (endB + startB + 1) / 2);
}
else {
return getMedian(a, b, startA, (endB + startB + 1) / 2,
(endA + startA + 1) / 2, endB);
}
}
public static void main(String[] args) {
int arr1[] = {1, 13, 25, 40, 50};
int arr2[] = {60, 70, 80, 90, 100};
/*
Merged array: 1, 2, 13, 17, 25, 30, 40, 45, 50, 70
(25 + 30) / 2 = 27
*/
int n1 = arr1.length;
int n2 = arr2.length;
long startTime = System.nanoTime();
if(n1 == n2) {
int median = getMedian(arr1, arr2, 0, 0, n1 - 1, n2 - 1);
System.out.println(median);
}
else {
System.out.println("Size of arrays are not equal");
}
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
System.out.println(totalTime);
}
}