Open In App

Making elements distinct in a sorted array by minimum increments

Last Updated : 19 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Implementation:

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) 

Auxiliary Space: O(1)

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 .

Implementation:

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) 

Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads