Fewest distinct integers left after removing exactly k elements

Quick Overview

Given an integer array and a budget k, find the smallest number of distinct values that can remain after deleting exactly k elements. The problem tests frequency counting with a hash map, choosing which values to eliminate first, and proving that choice is optimal.

Fewest distinct integers left after removing exactly k elements

Company: McKinsey

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an array of integers `arr` and an integer `k`. Remove exactly `k` elements from `arr` so that as few distinct integers as possible remain, and return that smallest possible number of distinct integers. ### Function Signature ```python def find_least_num_of_unique_ints(arr: list[int], k: int) -> int: ``` ### Rules - Each removal deletes one element, that is, one occurrence of a value. Making a value disappear completely takes as many removals as it has occurrences. - You may remove elements from any positions. The order of the remaining elements does not matter. - If every element is removed, the answer is 0. ### Constraints - `1 <= len(arr) <= 10^5` - `1 <= arr[i] <= 10^5` - `0 <= k <= len(arr)` ### Examples ```text Input: arr = [5, 5, 4], k = 1 Output: 1 ``` Removing the single 4 leaves `[5, 5]`, which has one distinct integer. Removing a 5 instead would leave both 4 and 5. ```text Input: arr = [4, 3, 1, 1, 3, 3, 2], k = 3 Output: 2 ``` Removing 4, 2 and one of the two 1s leaves `[3, 1, 3, 3]`, whose distinct values are 1 and 3. Getting down to a single distinct value would take at least 4 removals (keep only the three 3s), so 2 is the best possible. ```text Input: arr = [1, 2, 3], k = 3 Output: 0 ``` Here `k` equals the array length, so every element is removed and nothing remains.

Overview: Given an integer array and a budget k, find the smallest number of distinct values that can remain after deleting exactly k elements. The problem tests frequency counting with a hash map, choosing which values to eliminate first, and proving that choice is optimal.

McKinsey logo
McKinsey
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of integers arr and an integer k. Remove exactly k elements from arr so that as few distinct integers as possible remain, and return that smallest possible number of distinct integers.

Function Signature

def find_least_num_of_unique_ints(arr: list[int], k: int) -> int:

Rules

  • Each removal deletes one element, that is, one occurrence of a value. Making a value disappear completely takes as many removals as it has occurrences.
  • You may remove elements from any positions. The order of the remaining elements does not matter.
  • If every element is removed, the answer is 0.

Constraints

  • 1 <= len(arr) <= 10^5
  • 1 <= arr[i] <= 10^5
  • 0 <= k <= len(arr)

Examples

Input: arr = [5, 5, 4], k = 1
Output: 1

Removing the single 4 leaves [5, 5], which has one distinct integer. Removing a 5 instead would leave both 4 and 5.

Input: arr = [4, 3, 1, 1, 3, 3, 2], k = 3
Output: 2

Removing 4, 2 and one of the two 1s leaves [3, 1, 3, 3], whose distinct values are 1 and 3. Getting down to a single distinct value would take at least 4 removals (keep only the three 3s), so 2 is the best possible.

Input: arr = [1, 2, 3], k = 3
Output: 0

Here k equals the array length, so every element is removed and nothing remains.