Minimum Deletions to Remove Half the Elements
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Tests greedy selection by frequency: choose the fewest distinct values whose total occurrences cover at least half the array.
You are given an array of integers arr. Your task is to select a subset of distinct values from the array and remove every occurrence of each selected value.
Your goal is to remove at least half of the original array elements. Return the minimum number of distinct values you must select to achieve this.
Rules
- When you select a distinct value, all of its occurrences in the array are removed (you cannot remove only some copies).
- "At least half" means the number of removed elements must satisfy:
2 × removed_count ≥ len(arr).- For arrays with odd length, this means you must remove at least
⌈len(arr) / 2⌉elements. - Example: if
len(arr) = 5, you must remove at least 3 elements.
- For arrays with odd length, this means you must remove at least
- Return the count of distinct values selected, not the values themselves.
Constraints
1 ≤ len(arr) ≤ 10⁵-10⁹ ≤ arr[i] ≤ 10⁹
Examples
Example 1
Input: min_deletions_to_remove_half([4, 4, 4, 7, 7, 2, 9, 9])
Output: 2
Example 2
Input: min_deletions_to_remove_half([3, 3, 8, 8, 5])
Output: 2
Example 3
Input: min_deletions_to_remove_half([1, 2, 3, 4, 5, 6])
Output: 3
Hints
Hint 1
Count how many times each distinct value appears in the array. The actual values don't matter, only their frequencies.
Hint 2
To minimize the number of selections, prioritize values that appear most frequently—each selection should remove as many elements as possible.
Hint 3
Sort the frequencies in descending order and greedily pick from the largest until you've removed enough elements to meet the threshold.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that we want to maximize the number of elements removed with each selection. Since selecting a value removes all its occurrences, we should prioritize values that appear most frequently.
Strategy:
- Count the frequency of each distinct value in the array.
- Sort these frequencies in descending order.
- Greedily select values starting from the highest frequency, accumulating the total removed count.
- Stop as soon as the removed count satisfies
2 × removed ≥ n, and return the number of selections made.
Why this works:
The greedy approach is optimal because of an exchange argument: if you have a solution using k selections that doesn't include one of the top-k most frequent values, you can swap it with a less frequent value in your selection set. This swap doesn't decrease the total removed count, proving that the top-k frequencies always give the maximum removal for k selections.
Therefore, the minimum k needed is exactly the smallest prefix of sorted frequencies whose sum meets the threshold.
Edge cases:
- Single element array: One selection removes it, satisfying
2 × 1 ≥ 1. - All elements identical: One selection removes everything.
- All elements distinct: Need
⌈n/2⌉selections. - Odd-length arrays: The condition
2 × removed ≥ ncorrectly handles rounding up.
Complexity
- Time: O(n + d log d), where
dis the number of distinct values. Counting takes O(n), sorting frequencies takes O(d log d). In the worst case (all distinct), this is O(n log n). - Space: O(d) for the frequency counter and sorted frequency list.
Solution
from collections import Counter
from typing import List
def min_deletions_to_remove_half(arr: List[int]) -> int:
n = len(arr)
removed = 0
selections = 0
for freq in sorted(Counter(arr).values(), reverse=True):
removed += freq
selections += 1
if 2 * removed >= n:
return selections
return selections