Reduce Array Size to The Half

You are given an integer array arr. Choose a set of integers and remove every occurrence of them from the array. Return the minimum size of such a set so that at least half of the array is removed.

30:00

Reduce Array Size to The Half
medium
Free written solution
Topics
Companies

You are given an integer array arr. Choose a set of integers and remove every occurrence of them from the array. Return the minimum size of such a set so that at least half of the array is removed.

Example 1:
Input: {"arr":[3,3,3,3,5,5,5,2,2,7]}
Output: 2
Constraints:
  • 2≤arr.length≤1052 \leq \text{arr.length} \leq 10^5

  • arr.length\text{arr.length} is even

  • 1≤arr[i]≤1051 \leq \text{arr}[i] \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr":[3,3,3,3,5,5,5,2,2,7]}

Remove whole values until at least 5 of the 10 elements are gone, using as few distinct values as possible.

arr (10 elements)

3

3

3

3

5

5

5

2

2

7

removed

0 / 10 (need 5)

half = 5

counts

picked piles are colored

no counts yet

values picked

0

Variables
VariableValue
removed0
chosen0
half5
DepthFunction Call
Stack empty
0/14