> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Minimum Increment to Make Array Unique

> Tested Python solution for LeetCode 945 with 14 pytest cases. Generate a practice environment with lcpy.

LeetCode 945, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/minimum-increment-to-make-array-unique/description/).

Generate this problem as a practice environment: tested reference solution, 14 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 945   # by problem number
lcpy gen -s minimum_increment_to_make_array_unique   # by problem name
```

## Problem

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

\<p>Return \<em>the minimum number of moves to make every value in \</em>\<code>nums\</code>\<em> \<strong>unique\</strong>\</em>.\</p>

\<p>The test cases are generated so that the answer fits in a 32-bit integer.\</p>

### Examples

```
Input: nums = [1,2,2]
Output: 1
Explanation: After 1 move, the array could be [1, 2, 3].
```

```
Input: nums = [3,2,1,2,1,7]
Output: 6
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7].
It can be shown that it is impossible for the array to have all unique values with 5 or less moves.
```

### Constraints

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_increment_to_make_array_unique/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/minimum_increment_to_make_array_unique/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n) for the sorted copy
    def min_increment_for_unique(self, nums: list[int]) -> int:
        moves = 0
        previous = -1
        for num in sorted(nums):
            if num <= previous:
                # Raise num to one above the last placed value
                moves += previous - num + 1
                previous += 1
            else:
                previous = num
        return moves
```

## Complexity

| Time | Space |
| - | - |
| O(n log n) | O(n) for the sorted copy |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.