Skip to content
Related Articles

Related Articles

Improve Article

Making elements distinct in a sorted array by minimum increments

  • Difficulty Level : Medium
  • Last Updated : 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++




// 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 code
int main()
{
    int arr[] = { 2, 2, 3, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    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[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
// 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
?>

Javascript




<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++




// 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 code
int main()
{
    int arr[] = { 2, 2, 3, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
    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[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




<?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.
?>

Javascript




<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 code
let 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) 
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :