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