Median of Two Sorted Arrays Leetcode Solution

Median of Two Sorted Arrays Leetcode Problem :

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).

Median of Two Sorted Arrays Leetcode Solution :

Constraints :

  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -106 <= nums1[i], nums2[i] <= 10^6

Example 1:

  • Input: nums1 = [1,2], nums2 = [3,4]
  • Output: 2.50000

Approach :

  • Create a new array with a size equal to the total number of elements in both input arrays.
  • Insert elements from both input arrays into the new array.
  • Sort the new array.
  • Find and return the median of the sorted array.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Code :

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription