Least Number of Unique Integers after K Removals

MediumGreedygreedy

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given an integer array arr and an integer k, remove **exactly** k elements from the array (any positions you like). Return the smallest possible number of distinct values left in the array afterwards. An O(n log n) solution is expected.

Examples

Input: arr = [5,5,4], k = 1
Output: 1
Remove the single 4, leaving only 5.
Input: arr = [4,3,1,1,3,3,2], k = 3
Output: 2
Remove 4 and 2 entirely (2 removals) and one copy of 1 or 3; values 1 and 3 remain.

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^9
  • 0 <= k <= arr.length

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More greedy problems