-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedianTwoArray.java
53 lines (43 loc) · 1.34 KB
/
MedianTwoArray.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
41
42
43
44
45
46
47
48
49
50
51
52
53
public class MedianTwoArray {
public static int[] findMedianSortedArrays(int[] nums1, int[] nums2) {
int m = nums1.length;
int n = nums2.length;
int[] mergearray = new int[m+n];
int i=0, j=0,k=0;
for(;i<m && j<n;k++){
if(nums1[i] <= nums2[j]){
mergearray[k] = nums1[i++];
}
else{
mergearray[k] = nums2[j++];
}
}
for (; i < m; i++, k++) {
mergearray[k] = nums1[i];
}
// Copy remaining elements from the second array, if any
for (; j < n; j++, k++) {
mergearray[k] = nums2[j];
}
return mergearray;
// return 0.0;
}
public static void main(String[] args) {
int[] nums1 = {1, 2};
int[] nums2 = {3,4};
int[] mergedArray = findMedianSortedArrays(nums1, nums2);
int l = mergedArray.length;
double median = 0;
// System.out.print("Mrged Array: ");
for (int i = 0; i < l; i++) {
if(mergedArray.length % 2 == 0){
double m = mergedArray[l/2]+mergedArray[(l/2)-1];
median = m/2;
}
else{
median = mergedArray[l/2];
}
}
System.out.println(median);
}
}