# Making elements distinct in a sorted array by minimum increments

Given a sorted integer array. We need to make array elements distinct by increasing values and keeping array sum minimum possible. We need to print the minimum possible sum as output.

Examples:

```Input : arr[] = { 2, 2, 3, 5, 6 } ;
Output : 20
Explanation : We make the array as {2,
3, 4, 5, 6}. Sum becomes 2 + 3 + 4 +
5 + 6 = 20

Input : arr[] = { 20, 20 } ;
Output : 41
Explanation : We make {20, 21}

Input :  arr[] = { 3, 4, 6, 8 };
Output : 21
Explanation : All elements are unique
so result is sum of each elements.
```

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

Method 1:
1. Traverse each element of array .
2. if arr[i] == arr[i-1] then update each element of array by adding 1 from i-th(current) position to where element is either equal to its previous element or has become less than previous (because previous was increased).
3. After traversing of each element return sum .

## C++

 `// CPP program to make sorted array elements ` `// distinct by incrementing elements and keeping ` `// sum to minimum. ` `#include ` `using` `namespace` `std; ` ` `  `// To find minimum sum of unique elements. ` `int` `minSum(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `sum = arr; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) { ` `        ``if` `(arr[i] == arr[i - 1]) {             ` ` `  `            ``// While current element is same as  ` `            ``// previous or has become smaller ` `            ``// than previous. ` `            ``int` `j = i; ` `            ``while` `(j < n && arr[j] <= arr[j - 1]) {           ` `                ``arr[j] = arr[j] + 1; ` `                ``j++; ` `            ``} ` `        ``} ` `         ``sum = sum + arr[i]; ` `     ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 2, 3, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << minSum(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to make sorted  ` `// array elements distinct by  ` `// incrementing elements and  ` `// keeping sum to minimum. ` `import` `java.io.*; ` ` `  `class` `GFG  ` `{ ` `    ``// To find minimum sum  ` `    ``// of unique elements. ` `    ``static` `int` `minSum(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `sum = arr[``0``]; ` `     `  `        ``for` `(``int` `i = ``1``; i < n; i++)  ` `        ``{ ` `            ``if` `(arr[i] == arr[i - ``1``]) {          ` `     `  `                ``// While current element is same as  ` `                ``// previous or has become smaller ` `                ``// than previous. ` `                ``int` `j = i; ` `                ``while` `(j < n && arr[j] <= arr[j - ``1``])  ` `                ``{          ` `                    ``arr[j] = arr[j] + ``1``; ` `                    ``j++; ` `                ``} ` `            ``} ` `            ``sum = sum + arr[i]; ` `        ``} ` `     `  `        ``return` `sum; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `main (String[] args)  ` `    ``{ ` `        ``int` `arr[] = { ``2``, ``2``, ``3``, ``5``, ``6` `}; ` `        ``int` `n = arr.length; ` `        ``System.out.println(minSum(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Ansu Kumari `

## Python3

 `# Python3 program to make sorted array elements ` `# distinct by incrementing elements and keeping ` `# sum to minimum. ` ` `  `# To find minimum sum of unique elements. ` `def` `minSum(arr, n): ` `    ``sm ``=` `arr[``0``] ` ` `  `    ``for` `i ``in` `range``(``1``, n): ` `        ``if` `arr[i] ``=``=` `arr[i ``-` `1``]:          ` ` `  `            ``# While current element is same as  ` `            ``# previous or has become smaller ` `            ``# than previous. ` `            ``j ``=` `i ` `            ``while` `j < n ``and` `arr[j] <``=` `arr[j ``-` `1``]:          ` `                ``arr[j] ``=` `arr[j] ``+` `1` `                ``j ``+``=` `1` ` `  `        ``sm ``=` `sm ``+` `arr[i] ` ` `  `    ``return` `sm ` ` `  `# Driver code ` `arr ``=` `[ ``2``, ``2``, ``3``, ``5``, ``6` `] ` `n ``=` `len``(arr) ` `print``(minSum(arr, n)) ` ` `  `# This code is contributed by Ansu Kumari `

## C#

 `// C# program to make sorted  ` `// array elements distinct by  ` `// incrementing elements and  ` `// keeping sum to minimum. ` `using` `System; ` ` `  `class` `GFG  ` `{ ` `    ``// To find minimum sum  ` `    ``// of unique elements. ` `    ``static` `int` `minSum(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``int` `sum = arr; ` `     `  `        ``for` `(``int` `i = 1; i < n; i++)  ` `        ``{ ` `            ``if` `(arr[i] == arr[i - 1]) {      ` `     `  `                ``// While current element is same as  ` `                ``// previous or has become smaller ` `                ``// than previous. ` `                ``int` `j = i; ` `                ``while` `(j < n && arr[j] <= arr[j - 1])  ` `                ``{      ` `                    ``arr[j] = arr[j] + 1; ` `                    ``j++; ` `                ``} ` `            ``} ` `            ``sum = sum + arr[i]; ` `        ``} ` `     `  `        ``return` `sum; ` `    ``} ` `     `  `    ``// Driver code ` `    ``public` `static` `void` `Main ()  ` `    ``{ ` `        ``int` `[]arr = { 2, 2, 3, 5, 6 }; ` `        ``int` `n = arr.Length; ` `        ``Console.WriteLine(minSum(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 ` `

Output :

```20
```

Time Complexity : O(n^2)

Method 2:
1. Traverse each element of array .
2. If arr[i] <= prev then update prev by adding 1 and update sum by adding prev,
else update prev by cur element and update sum by adding cur element(arr[i]).
3. After traversing of each element return sum .

## C++

 `// Efficient CPP program to make sorted array  ` `// elements distinct by incrementing elements  ` `// and keeping sum to minimum. ` `#include ` `using` `namespace` `std; ` ` `  `// To find minimum sum of unique elements. ` `int` `minSum(``int` `arr[], ``int` `n) ` `{ ` `    ``int` `sum = arr, prev = arr; ` ` `  `    ``for` `(``int` `i = 1; i < n; i++) { ` ` `  `        ``// If violation happens, make current ` `        ``// value as 1 plus previous value and ` `        ``// add to sum. ` `        ``if` `(arr[i] <= prev) { ` `            ``prev = prev + 1; ` `            ``sum = sum + prev; ` `        ``} ` ` `  `        ``// No violation. ` `        ``else` `{ ` `            ``sum = sum + arr[i]; ` `            ``prev = arr[i]; ` `        ``} ` `    ``} ` ` `  `    ``return` `sum; ` `} ` ` `  `// Drivers code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 2, 2, 3, 5, 6 }; ` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr); ` `    ``cout << minSum(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Efficient Java program to make sorted array  ` `// elements distinct by incrementing elements  ` `// and keeping sum to minimum. ` `import` `java.io.*; ` ` `  `class` `GFG { ` ` `  `    ``// To find minimum sum of unique elements. ` `    ``static` `int` `minSum(``int` `arr[], ``int` `n) ` `    ``{ ` `        ``int` `sum = arr[``0``], prev = arr[``0``]; ` `     `  `        ``for` `(``int` `i = ``1``; i < n; i++) { ` `     `  `            ``// If violation happens, make current ` `            ``// value as 1 plus previous value and ` `            ``// add to sum. ` `            ``if` `(arr[i] <= prev) { ` `                ``prev = prev + ``1``; ` `                ``sum = sum + prev; ` `            ``} ` `     `  `            ``// No violation. ` `             `  `            ``else` `{ ` `                ``sum = sum + arr[i]; ` `                ``prev = arr[i]; ` `            ``} ` `        ``} ` `     `  `        ``return` `sum; ` `    ``} ` `     `  `    ``// Drivers code ` `    ``public` `static` `void` `main (String[] args) { ` `     `  `        ``int` `arr[] = { ``2``, ``2``, ``3``, ``5``, ``6` `}; ` `        ``int` `n = arr.length; ` `         `  `        ``System.out.println(minSum(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Ansu Kumari. `

## Python3

 `# Efficient Python program to make sorted array  ` `# elements distinct by incrementing elements  ` `# and keeping sum to minimum. ` ` `  `# To find minimum sum of unique elements ` `def` `minSum(arr, n): ` `     `  `    ``sum` `=` `arr[``0``]; prev ``=` `arr[``0``] ` ` `  `    ``for` `i ``in` `range``(``1``, n): ` ` `  `        ``# If violation happens, make current ` `        ``# value as 1 plus previous value and ` `        ``# add to sum. ` `        ``if` `arr[i] <``=` `prev: ` `            ``prev ``=` `prev ``+` `1` `            ``sum` `=` `sum` `+` `prev ` ` `  `        ``# No violation. ` `        ``else` `: ` `            ``sum` `=` `sum` `+` `arr[i] ` `            ``prev ``=` `arr[i] ` ` `  `    ``return` `sum` ` `  `# Drivers code ` `arr ``=` `[ ``2``, ``2``, ``3``, ``5``, ``6` `] ` `n ``=` `len``(arr) ` `print``(minSum(arr, n)) ` ` `  `# This code is contributed by Ansu Kumari `

## C#

 `// Efficient C# program to make sorted array  ` `// elements distinct by incrementing elements  ` `// and keeping sum to minimum. ` `using` `System; ` ` `  `class` `GFG { ` ` `  `    ``// To find minimum sum of unique elements. ` `    ``static` `int` `minSum(``int` `[]arr, ``int` `n) ` `    ``{ ` `        ``int` `sum = arr, prev = arr; ` `     `  `        ``for` `(``int` `i = 1; i < n; i++) { ` `     `  `            ``// If violation happens, make current ` `            ``// value as 1 plus previous value and ` `            ``// add to sum. ` `            ``if` `(arr[i] <= prev) { ` `                ``prev = prev + 1; ` `                ``sum = sum + prev; ` `            ``} ` `     `  `            ``// No violation. ` `             `  `            ``else` `{ ` `                ``sum = sum + arr[i]; ` `                ``prev = arr[i]; ` `            ``} ` `        ``} ` `     `  `        ``return` `sum; ` `    ``} ` `     `  `    ``// Drivers code ` `    ``public` `static` `void` `Main () { ` `     `  `        ``int` `[]arr = { 2, 2, 3, 5, 6 }; ` `        ``int` `n = arr.Length; ` `         `  `        ``Console.WriteLine(minSum(arr, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m . `

## PHP

 ` `

Output:

```20
```

Time Complexity: O(n)

My Personal Notes arrow_drop_up 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 : jit_t, vt_m, Prashant Nigam

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.