Ctrl K

Make Every Number Distinct

Medium
The runners in a race were handed numbers, and badges[i] is the number of runner i. Due to a mix-up, some numbers repeat. In one step you can take any number and increase it by 1. Numbers can never go down. Return the smallest number of steps after which all numbers are pairwise distinct.
Example 1
badges=[4, 4, 4, 1] → 3

Keep one 4, raise the second to 5 (1 step) and the third to 6 (2 steps), 3 in total.

Example 2
badges=[2, 0, 2, 0, 1] → 5

One way is 2, 0, 3, 1, 4: the second 2 becomes 3 (+1), the second 0 becomes 1 (+1) and the 1 becomes 4 (+3), 5 in total.

Example 3
badges=[3, 9, 6] → 0

The numbers are already distinct.

Constraints
  • 1 ≤ len(badges) ≤ 10⁵
  • 0 ≤ badges[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsJobs With a Cooldown MediumSpread Out the Labels MediumPick Within Tag Limits MediumFewest Kinds After Removals MediumPalindromes From Shared Letters Medium
loading Python…
Input
badges=[4, 4, 4, 1]
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 107 tests on the server, hidden ones included.