In the Leetcode Median of Two Sorted Arrays problem solution in Java programming Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Leetcode Median of Two Sorted Arrays problem solution in Java programming
class Solution {
public double findMedianSortedArrays(int[] nums1, int[] nums2) {
ArrayList<Integer> arrayList=new ArrayList<>();
int l1=nums1.length,l2=nums2.length;
for(int i=0;i<l1;i++)
arrayList.add(nums1[i]);
for(int i=0;i<l2;i++)
arrayList.add(nums2[i]);
Collections.sort(arrayList);
int total=l1+l2;
return total%2==0?(double)(arrayList.get(total/2)+(double)arrayList.get(total/2-1))/2.00:arrayList.get(total/2);
}
}
Also read,
- Leetcode Median of Two Sorted Arrays problem solution in C
- Leetcode Median of Two Sorted Arrays problem solution in C++
- Leetcode Median of Two Sorted Arrays problem solution in Python
- Leetcode Median of Two Sorted Arrays problem solution in C#