<p>You are given an integer array <code>nums</code>. In one move, you can pick an index <code>i</code> where <code>0 &lt;= i &lt; 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>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> After 1 move, the array could be [1, 2, 3].
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [3,2,1,2,1,7]
<strong>Output:</strong> 6
<strong>Explanation:</strong> 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.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution {
public:
    int minIncrementForUnique(vector<int>& nums) {
        int n=nums.size();
        sort(nums.begin(),nums.end());
        int count=0;
        int i=0;
        while(i<n){
            int num=nums[i];
            while(i<n && nums[i]==num){
                i++;
            }
            count++;
        }
        count=n-count;
        vector<int> vect;
        i=0;
        int cur=nums[i];
        int distinct=0;
        int dup=0;
        while(vect.size()<count){
            //int num=cur;
            while( i<n && nums[i]==cur ){
                i++;
            }
            distinct++;
            dup=i-distinct;
            //cout<<"sssd   "<<dup<<"  "<<i<<"  "<<vect.size()<<endl;
            while(i<n && nums[i]!=cur+1 && dup-vect.size()>0){
                //cout<<"inside"<<endl;
                vect.push_back(cur+1);
                cur++;
            }
            if(i<n)
               cur=nums[i];
            if(i==n){
                cur++;
                while(vect.size()<count){
                    vect.push_back(cur);
                    cur++;
                }
            }

        }
      /*  cout<<"count  "<<count<<endl;
        cout<<"---------------"<<endl;
        for(auto v:vect){
            cout<<v<<"  ";
        }
        cout<<endl;*/
        //int i=0;
        int j=0;
        int ans=0;
        for(int i=1;i<n;i++){
            if(nums[i]==nums[i-1]){
                ans+=vect[j]-nums[i];
                j++;
            }
        }
        return ans;

    }
};
71630