Making elements distinct in a sorted array by minimum increments
Last Updated :
19 Sep, 2022
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:
- Traverse each element of array .
- 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).
- 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>
Time Complexity : O(n^2)
Auxiliary Space: O(1)
Method 2:
- Traverse each element of array .
- 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]). - 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>
Time Complexity: O(n)
Auxiliary Space: O(1)
Similar Reads
Print all Distinct (Unique) Elements in given Array Given an integer array arr[], print all distinct elements from this array. The given array may contain duplicates and the output should contain every element only once.Examples: Input: arr[] = {12, 10, 9, 45, 2, 10, 10, 45}Output: {12, 10, 9, 45, 2}Input: arr[] = {1, 2, 3, 4, 5}Output: {1, 2, 3, 4,
11 min read
Print sorted distinct elements of array Given an array that might contain duplicates, print all distinct elements in sorted order. Examples: Input : 1, 3, 2, 2, 1Output : 1 2 3Input : 1, 1, 1, 2, 2, 3Output : 1 2 3The simple Solution is to sort the array first, then traverse the array and print only first occurrences of elements. Algorith
6 min read
Minimum increment/decrement to make array non-Increasing Given an array a, your task is to convert it into a non-increasing form such that we can either increment or decrement the array value by 1 in the minimum changes possible. Examples : Input : a[] = {3, 1, 2, 1}Output : 1Explanation : We can convert the array into 3 1 1 1 by changing 3rd element of a
11 min read
Minimize sum of distinct elements of all prefixes by rearranging Array Given an array arr[] with size N, the task is to find the minimum possible sum of distinct elements over all the prefixes of the array that can be obtained by rearranging the elements of the array. Examples: Input: arr[] = {3, 3, 2, 2, 3}, N = 5Output: 7Explanation: The permutation arr[] = {3, 3, 3,
8 min read
Minimum number of distinct elements present in a K-length subsequence in an array Given an array A[] consisting of N integers and an integer K, the task is to count the minimum number of distinct elements present in a subsequence of length K of the given array, A. Examples: Input: A = {3, 1, 3, 2, 3, 4, 5, 4}, K = 4Output: 2Explanation: The subsequence of length 4 containing mini
7 min read
Maximum distinct elements after removing k elements Given an array arr[] containing n elements. The task is to find the maximum count of distinct elements (non-repeating) after removing k elements from the array. Note: 1 <= k <= n.Examples: Input : arr[] = [5, 7, 5, 5, 1, 2, 2], k = 3Output : 4Explanation: Remove 2 occurrences of element 5 and
12 min read
JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu
3 min read
Minimum number of distinct elements after removing m items Given an array of items, an i-th index element denotes the item id's, and given a number m, the task is to remove m elements such that there should be minimum distinct id's left. Print the number of distinct id's. Examples: Input : arr[] = { 2, 2, 1, 3, 3, 3} m = 3Output : 1Remove 1 and both 2's.So,
15+ min read
Maximize count of unique array elements by incrementing array elements by K Given an array arr[] consisting of N integers and an integer K, the task is to find the maximum number of unique elements possible by increasing any array element by K only once. Examples: Input: arr[] = {0, 2, 4, 3, 4}, K = 1Output: 5Explanation:Increase arr[2] ( = 4) by K ( = 1). Therefore, new ar
8 min read
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2 Given an array arr[] of size N, the task is to print the minimum number of moves required to make all array elements equal by selecting any pair of distinct indices and then increment the element at the first index and decrement the element at the other index by 1 each time. If it is impossible to m
7 min read