FastPrepFastPrep
Problem Brief

Get Minimum Removals

NEW GRADOA

Amazon's data analysts are currently working on an exciting new prototype that focuses on data pruning. In this challenge, you are presented with an array of integers named numbers, containing n elements, and an integer called allowedDistinct. The objective is to strategically remove as few elements as possible from the numbers array so that the remaining dataset contains no more than allowedDistinct unique values.

In other words, given the input array data and the parameter max_distinct, your challenge is to determine the minimum number of elements that must be removed so that the resulting array has at most max_distinct unique elements.

To accomplish this, you need to complete the function getMinRemovals with the following parameters:

  • int numbers[n]: The input array of integer data.
  • int allowedDistinct: The maximum number of distinct elements allowed in the pruned array.
  • The function should return an integer representing the minimum number of elements that must be removed to achieve an array with at most allowedDistinct unique elements.

    1Example 1

    Input
    numbers = [1, 2, 3, 2, 1], alllowedDistinct = 2
    Output
    1
    Explanation
    Example 1 illustration
    The best approach is to remove just one element—the number 3—from the array. This results in the sequence [1, 2, 2, 1], which contains only 2 distinct elements. Therefore, the minimum number of removals required is 1.

    Constraints

    Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 10^5
  • 1 ≤ numbers[i] ≤ 10^9
  • 1 ≤ allowedDistinct ≤ 10^5
  • public int getMinRemovals(int[] numbers, int allowedDistinct) {
      // write your code here
    }
    
    Input

    numbers

    [1, 2, 3, 2, 1]

    alllowedDistinct

    2

    Output

    1

    Sign in to submit your solution.