Leetcode Median of Two Sorted Arrays problem solution in Java programming

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,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *