Possible Medians After K Deletions
You are given an array nums of N integers and an integer K. You need to remove exactly K elements from the array. After the deletions, the remaining array will have a size of M = N - K.
Return an array all distinct values that can possibly be the median of the remaining array sorted in non-decreasing order.
Example 1:
Input: nums = [10, 20, 30, 40, 50], K = 2
Output: [20, 30, 40]
Explanation:
After removing 2 elements, M = 3
Median position = ceil(3 / 2) = 2
Possible cases:
- Remove {40, 50} → [10, 20, 30], median = 20
- Remove {10, 50} → [20, 30, 40], median = 30
- Remove {10, 20} → [30, 40, 50], median = 40
All other removals give one of these values.
Example 2:
Input: nums = [1, 5, 5, 5, 9], K = 1
Output: [5]
Explanation: M = 4, Median position = ceil(4 / 2) = 2
No matter which element is removed, the 2nd smallest element is always 5.
Now Your Turn!
Pick the correct output for the given inputInput: nums = [1, 2, 3, 4, 5, 6], K = 3
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ K < N ≤ 100
- 1 ≤ nums[i] ≤ 1e9