FastPrepReduce Array Size to At Most Half

Reduce Array Size to At Most Half

Microsoft logoMicrosoft● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Choose a set of distinct integer values and remove every occurrence of each chosen value from values. Return the minimum number of chosen values needed so that the remaining array has at most half of its original length, using integer floor for the allowed remaining length.

Function

minValuesToRemoveHalf(values: int[]) → int

Examples

Example 1

values = [3,3,3,3,5,5,5,2,2,7]return = 2

Removing all 3s and all 5s removes seven elements.

Example 2

values = [7,7,7,7]return = 1

One selected value removes the whole array.

Constraints

  • 1 <= values.length <= 200000
  • Each value fits in a signed 32-bit integer.

More Microsoft problems

See Microsoft hiring insights
public int minValuesToRemoveHalf(int[] values) {
    // Write your code here.
}
values[3,3,3,3,5,5,5,2,2,7]
expected2
Checking account…