Making elements distinct in a sorted array by minimum increments

03 May, 2021
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.  
 
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++
Java
Python3
C#
PHP
Javascript
// CPP program to make sorted array elements// distinct by incrementing elements and keeping// sum to minimum.#include <iostream>using namespace std; // To find minimum sum of unique elements.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 codeint main(){    int arr[] = { 2, 2, 3, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << minSum(arr, n) << endl;    return 0;}
// 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 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 codearr = [ 2, 2, 3, 5, 6 ]n = len(arr)print(minSum(arr, n)) # This code is contributed by Ansu Kumari
// 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[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 ()    {        int []arr = { 2, 2, 3, 5, 6 };        int n = arr.Length;        Console.WriteLine(minSum(arr, n));    }} // This code is contributed by vt_m
<?php// PHP program to make sorted array// elements distinct by incrementing// elements and keeping sum to minimum. // To find minimum sum of unique// elements.function minSum($arr, $n){    $sum = $arr[0];     for ($i = 1; $i < $n; $i++) {        if ($arr[$i] == $arr[$i - 1])        {                 // While current element is            // same as previous or has            // become smaller than            // previous.            $j = $i;            while ($j < $n && $arr[$j]                        <= $arr[$j - 1])            {                    $arr[$j] = $arr[$j] + 1;                $j++;            }        }        $sum = $sum + $arr[$i];    }     return $sum;} // Driver code    $arr = array ( 2, 2, 3, 5, 6 );    $n = sizeof($arr) ;    echo minSum($arr, $n),"\n"; // This code is contributed by ajit?>
<script> // JavaScript program to make sorted// array elements distinct by// incrementing elements and// keeping sum to minimum.     // To find minimum sum    // of unique elements.    function minSum(arr, n)    {        let sum = arr[0];               for (let i = 1; i < n; i++)        {            if (arr[i] == arr[i - 1]) {                               // While current element is same as                // previous or has become smaller                // than previous.                let j = i;                while (j < n && arr[j] <= arr[j - 1])                {                            arr[j] = arr[j] + 1;                    j++;                }            }            sum = sum + arr[i];        }               return sum;    } // Driver code         let arr = [ 2, 2, 3, 5, 6 ];        let n = arr.length;        document.write(minSum(arr, n));            </script>
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++
Java
Python3
C#
PHP
Javascript
// Efficient CPP program to make sorted array// elements distinct by incrementing elements// and keeping sum to minimum.#include <iostream>using namespace std; // To find minimum sum of unique elements.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 codeint main(){    int arr[] = { 2, 2, 3, 5, 6 };    int n = sizeof(arr) / sizeof(arr[0]);    cout << minSum(arr, n) << endl;    return 0;}
// 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.
# Efficient Python program to make sorted array# elements distinct by incrementing elements# and keeping sum to minimum. # To find minimum sum of unique elementsdef 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 codearr = [ 2, 2, 3, 5, 6 ]n = len(arr)print(minSum(arr, n)) # This code is contributed by Ansu Kumari
// 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[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 () {             int []arr = { 2, 2, 3, 5, 6 };        int n = arr.Length;                 Console.WriteLine(minSum(arr, n));    }} // This code is contributed by vt_m .
<?php// Efficient PHP program to// make sorted array elements// distinct by incrementing // elements and keeping sum// to minimum. // To find minimum sum// of unique elements.function minSum($arr, $n){    $sum = $arr[0];    $prev = $arr[0];     for ( $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;} // Driver code$arr = array(2, 2, 3, 5, 6);$n = count($arr);echo minSum($arr, $n); // This code is contributed by anuj_67.?>
<script> // Efficient Javascript program to make sorted array// elements distinct by incrementing elements// and keeping sum to minimum. // To find minimum sum of unique elements.function minSum(arr, n){    let sum = arr[0], prev = arr[0];      for(let 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;} // Driver codelet arr = [ 2, 2, 3, 5, 6 ];let n = arr.length; document.write(minSum(arr, n)); // This code is contributed by decode2207 </script>
Output:  
20
Time Complexity: O(n)  
jit_t
vt_m
Prashant Nigam
susmitakundugoaldanga
decode2207
Arrays
Searching
Arrays
Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Binary Search
Maximum and minimum of an array using minimum number of comparisons
Linear Search
K'th Smallest/Largest Element in Unsorted Array | Set 1
Search an element in a sorted and rotated array