Skip to main content

Weighted Median Split Finder

hard
ArrayBinary SearchDivide And ConquerBinary Search On Answer
Asked atGoogleAmazonMicrosoftMetaApple

Problem Description

You are given two sorted integer arrays `leftNums` and `rightNums` of lengths `m` and `n` respectively. Find the **median** of the combined sorted sequence formed by merging both arrays.

The median is defined as:
- If the total number of elements is odd, the middle element.
- If even, the average of the two middle elements.

You must solve this in **O(log(min(m, n)))** time.

**Example 1:**
```
Input: leftNums = [1, 4, 7], rightNums = [2, 3, 8, 10]
Output: 4.0
Explanation: Merged: [1, 2, 3, 4, 7, 8, 10] (7 elements). Median = 4.
```

**Example 2:**
```
Input: leftNums = [3, 9], rightNums = [1, 5, 11, 13]
Output: 7.0
Explanation: Merged: [1, 3, 5, 9, 11, 13] (6 elements). Median = (5+9)/2 = 7.0.
```

Constraints

  • 0 <= leftNums.length <= 1000
  • 0 <= rightNums.length <= 1000
  • leftNums.length + rightNums.length >= 1
  • -10^6 <= leftNums[i], rightNums[i] <= 10^6
  • leftNums and rightNums are each sorted in non-decreasing order

Follow-up

Generalise to find the k-th smallest element among k sorted arrays of arbitrary sizes in O(k log(max_len)) time.

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.