I copy the length of two initial arrays and then merge them into a third array and the length of the third array is equal to the length of initial two arrays added together. Then I try to find the median of the merged array.
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
int[] a = nums1;
int[] b = nums2;
int c = a.length + b.length;
int[] myArr = new int[c];
for (int i = 0; i < a.length; i++) {
myArr[i] = a[i];
}
for (int i = 0; i < b.length; i++) {
myArr[a.length + i] = b[i];
}
Arrays.sort(myArr);
double median;
int lastIndex = myArr.length - 1;
int middle = lastIndex / 2;
if (myArr.length % 2 != 0) {
median = myArr[middle];
} else {
median = (myArr[middle] + myArr[middle + 1]) / 2.0;
}
return median;
}
}