# Minimum Increment operations to make Array unique

Given an array A[] of integers. In one move you can choose any element A[i], and increment it by 1. The task is to return the minimum number of moves needed to make every value in the array A[] unique.

Examples:

Input: A[] = [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.

Input: A[] = [1, 2, 2]
Output: 1
Explanation: After 1 move [2 -> 3], the array could be [1, 2, 3].

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution to make each duplicate values unique is to keep incrementing it repeatedly until it is not unique. However, we might do a lot of extra work, if we have an array of all ones.

So, what we can do instead is to evaluate what our increments should be. If for example we have [1, 1, 1, 3, 5], we don’t need to process all the increments of duplicated 1’s. We could take two ones (taken = [1, 1]) and continue processing. Whenever we find an empty(unused value) place like 2 or 4 we can then recover that our increment will be 2-1, 4-1 respectively.

Thus, we first count the values and for each possible value X in array:

• If there are 2 or more values X in A, save the extra duplicated values to increment later.
• If there are 0 values X in A, then a saved value gets incremented to X.

Below is the implementation of above approach:

 // C++ Implementation of above approach #include    using namespace std;    // function to find minimum increment required int minIncrementForUnique(vector A) {        // collect frequency of each element     unordered_map mpp;        for(int i:A) mpp[i]++;        // array of unique values taken     vector taken;        int ans = 0;        for (int x = 0; x < 100000; x++)     {         if (mpp[x] >= 2)             taken.push_back(x * (mpp[x] - 1));         else if(taken.size() > 0 and mpp[x] == 0)         {             ans += x - taken.back();             taken.pop_back();         }     }        // return answer     return ans; }    // Driver code int main() {        vector A = {3, 2, 1, 2, 1, 7};            cout << minIncrementForUnique(A);            return 0; }    // This code is contributed by mohit kumar 29

 # Python Implementation of above approach    import collections    # function to find minimum increment required def minIncrementForUnique(A):        # collect frequency of each element     count = collections.Counter(A)        # array of unique values taken     taken = []        ans = 0        for x in range(100000):         if count[x] >= 2:             taken.extend([x] * (count[x] - 1))         elif taken and count[x] == 0:             ans += x - taken.pop()        # return answer     return ans    # Driver code A = [3, 2, 1, 2, 1, 7] print(minIncrementForUnique(A))

Output:
6

Time Complexity: O(N)

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : mohit kumar 29

Article Tags :
Practice Tags :