DSA · Binary Search · #115
Median of Two Sorted Arrays
Module 59 · difficulty 4/5·⏱ 30:00starts on first keystroke
Given two sorted integer arrays `nums1` and `nums2` of sizes `m` and `n`, return the median of the combined sorted array. Implement `findMedianSortedArrays(nums1, nums2)`. It returns a `number` (the median). If the total length `m + n` is even, the median is the average of the two middle values. The overall run time complexity should be `O(log(m + n))`.
Examples
nums1 = [1,3], nums2 = [2]→2.0— Merged = [1,2,3]; median is the middle element 2.nums1 = [1,2], nums2 = [3,4]→2.5— Merged = [1,2,3,4]; median is (2 + 3) / 2 = 2.5.nums1 = [], nums2 = [1]→1.0— One array is empty; median is the single element.
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
- · Both nums1 and nums2 are sorted in non-decreasing order
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.