Reduce Array Size to The Half - Problem

You are given an integer array arr. Your goal is to strategically remove elements to reduce the array size by at least half, but here's the catch: when you choose to remove a number, all occurrences of that number must be removed from the array.

Your mission: Find the minimum number of distinct integers you need to remove to achieve this goal.

For example, if you have the array [3,3,3,3,5,5,5,2,2,7] with 10 elements, you need to remove at least 5 elements. The smart strategy would be to remove all 3's (removes 4 elements) and all 5's (removes 3 elements), giving you 7 total removals with just 2 distinct numbers chosen.

Think of it as: You're a data cleanup specialist trying to minimize the number of different deletion operations while maximizing impact!

Input & Output

example_1.py โ€” Basic Case
$ Input: arr = [3,3,3,3,5,5,5,2,2,7]
โ€บ Output: 2
๐Ÿ’ก Note: We need to remove at least 5 elements (half of 10). The optimal strategy is to remove all 3's (4 elements) and all 5's (3 elements), totaling 7 removals with just 2 distinct numbers. This is better than other combinations like removing {3,2,7} which needs 3 distinct numbers.
example_2.py โ€” All Elements Same
$ Input: arr = [7,7,7,7,7,7]
โ€บ Output: 1
๐Ÿ’ก Note: All elements are the same, so removing one distinct number (7) removes all 6 elements, which is definitely at least half of 6. Only 1 distinct number needed.
example_3.py โ€” All Elements Unique
$ Input: arr = [1,9,3,2,17,8]
โ€บ Output: 3
๐Ÿ’ก Note: All elements appear exactly once. To remove at least 3 elements (half of 6), we need to remove 3 distinct numbers. Any combination of 3 numbers works, such as {1,9,3} or {2,17,8}.

Constraints

  • 1 โ‰ค arr.length โ‰ค 105
  • 1 โ‰ค arr[i] โ‰ค 105
  • arr.length is always even

Visualization

Tap to expand
๐Ÿช Cookie Jar Cleanup StrategyOriginal Jar: 10 cookies, need to remove โ‰ฅ 5CCCCCCCCOOOSSGCC=Chocolate Chip(4), O=Oatmeal(3), S=Sugar(2), G=Ginger(1)Step 1: Count & Sort by FrequencyChoc Chip: 4Oatmeal: 3Sugar: 2Ginger: 1Step 2: Greedy Selection ProcessRemove Chocolate Chip (4 cookies): total = 4 โŒ still need 1 moreRemove Oatmeal (3 cookies): total = 7 โœ… target reached!๐ŸŽฏ Result: Only 2 removal operations needed (2 cookie types)๐Ÿ’ก Key Insight: Greedy Works Perfectly!Always removing the most abundant cookie type first guaranteesthe minimum number of removal operations needed.
Understanding the Visualization
1
Count Cookie Types
Count how many cookies of each type: Chocolate Chip (4), Oatmeal (3), Sugar (2), Ginger (1)
2
Sort by Abundance
Arrange types by quantity: [4, 3, 2, 1] - most abundant first
3
Greedy Removal
Remove most abundant type (4 cookies), then next (3 cookies) = 7 total removed โ‰ฅ 5 needed
4
Count Operations
Only 2 removal operations needed (Chocolate Chip + Oatmeal types)
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because removing high-frequency elements first maximizes impact per operation, and there's never a scenario where choosing a less frequent element over a more frequent one leads to a better solution.
Asked in
Amazon 45 Facebook 38 Google 32 Microsoft 28 Apple 25
42.8K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen