Tutorialspoint
Problem
Solution
Submissions

Median of Two Sorted Arrays

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 12
I'll reformat these additional problems to match the sample format:

Write a C++ program to find the median of two sorted arrays. 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)).

Example 1
  • Input: nums1 = [1,3], nums2 = [2]
  • Output: 2.00000
  • Explanation:
    • Step 1: Design a binary search approach to find the correct partition point.
    • Step 2: Partition both arrays such that elements to the left form the first half of the merged array.
    • Step 3: Find the median using the maximum of left elements and minimum of right elements.
    • Step 4: For this example, the merged array would be [1,2,3] and the median is 2.
Example 2
  • Input: nums1 = [1,2], nums2 = [3,4]
  • Output: 2.50000
  • Explanation:
    • Step 1: Design a binary search approach to find the correct partition point.
    • Step 2: Partition both arrays such that elements to the left form the first half of the merged array.
    • Step 3: Find the median using the maximum of left elements and minimum of right elements.
    • Step 4: For this example, the merged array would be [1,2,3,4] and the median is (2 + 3) / 2 = 2.5.
Constraints
  • nums1.length == m
  • nums2.length == n
  • 0 ≤ m ≤ 1000
  • 0 ≤ n ≤ 1000
  • 1 ≤ m + n ≤ 2000
  • -10^6 ≤ nums1[i], nums2[i] ≤ 10^6
  • Time Complexity: O(log(min(m,n)))
  • Space Complexity: O(1)
ArraysFacebookTech Mahindra
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Binary search can be applied to the smaller array
  • The problem can be reframed as finding the correct partition point
  • Think about how medians divide arrays into equal halves
  • Consider both even and odd total lengths
  • Use the property that elements on the left side are all smaller than elements on the right side

Steps to solve by this approach:

 Step 1: Ensure we binary search on the smaller array (nums1) for efficiency.
 Step 2: For each partition in nums1 (partitionX), calculate the corresponding partition in nums2 (partitionY).
 Step 3: Find the maximum elements on the left side and minimum elements on the right side of both partitions.
 Step 4: Check if we've found the correct partition (maxX ≤ minY and maxY ≤ minX).
 Step 5: If correct partition is found, calculate median based on whether total length is odd or even.
 Step 6: Adjust the binary search range based on comparison of maxX and minY if needed.
 Step 7: Continue the binary search until the correct partition is found.

Submitted Code :