Fewest increment moves to make every value in an array distinct

Quick Overview

Given an integer array where each move adds 1 to a single element, find the fewest moves that leave every value distinct. The problem tests greedy reasoning after sorting, proving a greedy choice with an exchange argument, and handling large runs of repeated values within an O(n log n) budget.

Fewest increment moves to make every value in an array distinct

Company: ServiceNow

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given an integer array `nums`. In one move, you can pick an index `i` where `0 <= i < nums.length` and increment `nums[i]` by `1`. Return the minimum number of moves needed to make every value in `nums` unique. The test cases are generated so that the answer fits in a 32-bit integer. ### Function Signature ```python def min_increments_for_unique(nums: list[int]) -> int: ``` ### Rules - A move adds exactly 1 to one element. Values can only go up; there is no way to decrease an element. - An element may be incremented any number of times, and values may grow past the upper bound on the input values. - An array whose values are already all distinct needs 0 moves. ### Constraints - `1 <= nums.length <= 10^5` - `0 <= nums[i] <= 10^5` ### Examples ```text Input: nums = [1,2,2] Output: 1 ``` Sorted, the array is `[1, 2, 2]`. Incrementing the last 2 to 3 gives `[1, 2, 3]`, so 1 move is enough, and at least one move is needed because of the repeated 2. ```text Input: nums = [3,2,1,2,1,7] Output: 6 ``` Sorted, the array is `[1, 1, 2, 2, 3, 7]`. One optimal result is `[1, 2, 3, 4, 5, 7]`. Its sum is 22 and the original sum is 16, so it takes 6 moves. ```text Input: nums = [0,0,0,0] Output: 6 ``` The four zeros must become four distinct values, and the cheapest choice is `[0, 1, 2, 3]`, which costs `0 + 1 + 2 + 3 = 6` moves.

Overview: Given an integer array where each move adds 1 to a single element, find the fewest moves that leave every value distinct. The problem tests greedy reasoning after sorting, proving a greedy choice with an exchange argument, and handling large runs of repeated values within an O(n log n) budget.

ServiceNow logo
ServiceNow
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given an integer array nums. In one move, you can pick an index i where 0 <= i < nums.length and increment nums[i] by 1.

Return the minimum number of moves needed to make every value in nums unique.

The test cases are generated so that the answer fits in a 32-bit integer.

Function Signature

def min_increments_for_unique(nums: list[int]) -> int:

Rules

  • A move adds exactly 1 to one element. Values can only go up; there is no way to decrease an element.
  • An element may be incremented any number of times, and values may grow past the upper bound on the input values.
  • An array whose values are already all distinct needs 0 moves.

Constraints

  • 1 <= nums.length <= 10^5
  • 0 <= nums[i] <= 10^5

Examples

Input: nums = [1,2,2]
Output: 1

Sorted, the array is [1, 2, 2]. Incrementing the last 2 to 3 gives [1, 2, 3], so 1 move is enough, and at least one move is needed because of the repeated 2.

Input: nums = [3,2,1,2,1,7]
Output: 6

Sorted, the array is [1, 1, 2, 2, 3, 7]. One optimal result is [1, 2, 3, 4, 5, 7]. Its sum is 22 and the original sum is 16, so it takes 6 moves.

Input: nums = [0,0,0,0]
Output: 6

The four zeros must become four distinct values, and the cheapest choice is [0, 1, 2, 3], which costs 0 + 1 + 2 + 3 = 6 moves.