Minimum Increment operations to make Array unique
Last Updated :
02 Dec, 2024
Given an array arr[] of integers. In one operation you can choose an index i, and increment the element arr[i] by 1. The task is to return the minimum number of operations needed to make every value in the array arr[] unique.
Examples:
Input: arr[] = [3, 2, 1, 2, 1, 7]
Output: 6
Explanation: After 6 moves, the array could be [3, 4, 1, 2, 5, 7]. It can be shown that it is impossible for the array to have all unique values with 5 or less operations.
Input: arr[] = [1, 2, 2]
Output: 1
Explanation: After 1 operation [2 -> 3], the array could be [1, 2, 3].
Input: arr[] = [5, 4, 3, 2, 1]
Output: 0
Explanation: All elements are unique.
Expected Approach 1 - Use Sorting - O(n log(n)) Time and O(1) Space
The idea is to sort the array and then build a strictly increasing array by performing increment operations. Because elements in a strictly increasing array will always be unique.
Follow the given steps to solve the problem:
- Sort the array in increasing order and initialize cnt to 0.
- Starting from the second element, check if the current element is less than or equal to the previous element (Note that the previous element might become greater because of prior increment operations)
- If the current element is less than or equal to the previous element, update current element = previous element + 1 to make it strictly increasing. Also, add the number of increments to cnt.
Illustration:
C++
// C++ Program to find minimum increment operations
// to make array unique by sorting
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minIncrements(vector<int>& arr) {
// sort the array in increasing order
sort(arr.begin(), arr.end());
int cnt = 0;
for (int i = 1; i < arr.size(); i++) {
// If current element <= the previous element
if (arr[i] <= arr[i-1]) {
// Make the array strictly increasing
// by updating current element to
// previous element + 1
cnt += arr[i-1] + 1 - arr[i];
arr[i] = arr[i-1] + 1;
}
}
return cnt;
}
int main() {
vector<int> arr = {3, 2, 1, 2, 1, 7};
cout << minIncrements(arr);
}
C
// C Program to find minimum increment operations
// to make array unique by sorting
#include <stdio.h>
#include <stdlib.h>
// Comparator function for qsort
int compare(const void *a, const void *b) {
return (*(int *)a - *(int *)b);
}
int minIncrements(int arr[], int n) {
// sort the array in increasing order
qsort(arr, n, sizeof(int), compare);
int cnt = 0;
for (int i = 1; i < n; i++) {
// If current element <= the previous element
if (arr[i] <= arr[i - 1]) {
// Make the array strictly increasing
// by updating current element to
// previous element + 1;
cnt += arr[i - 1] + 1 - arr[i];
arr[i] = arr[i - 1] + 1;
}
}
return cnt;
}
int main() {
int arr[] = {3, 2, 1, 2, 1, 7};
int size = sizeof(arr) / sizeof(arr[0]);
printf("%d\n", minIncrements(arr, size));
return 0;
}
Java
// Java Program to find minimum increment operations
// to make array unique by sorting
import java.util.Arrays;
class GfG {
static int minIncrements(int[] arr) {
// sort the array in increasing order
Arrays.sort(arr);
int cnt = 0;
for (int i = 1; i < arr.length; i++) {
// If current element <= the previous element
if (arr[i] <= arr[i - 1]) {
// Make the array strictly increasing
// by updating current element to
// previous element + 1;
cnt += arr[i - 1] + 1 - arr[i];
arr[i] = arr[i - 1] + 1;
}
}
return cnt;
}
public static void main(String[] args) {
int[] arr = {3, 2, 1, 2, 1, 7};
System.out.println(minIncrements(arr));
}
}
Python
# Python Program to find minimum increment operations
# to make array unique by sorting
def minIncrements(arr):
# sort the array in increasing order
arr.sort()
cnt = 0
for i in range(1, len(arr)):
# If current element <= the previous element
if arr[i] <= arr[i - 1]:
# Make the array strictly increasing
# by updating current element to
# previous element + 1;
cnt += arr[i - 1] + 1 - arr[i]
arr[i] = arr[i - 1] + 1
return cnt
if __name__ == "__main__":
arr = [3, 2, 1, 2, 1, 7]
print(minIncrements(arr))
C#
// C# Program to find minimum increment operations
// to make array unique by sorting
using System;
using System.Collections.Generic;
class GfG {
static int minIncrements(int[] arr) {
// sort the array in increasing order
Array.Sort(arr);
int cnt = 0;
for (int i = 1; i < arr.Length; i++) {
// If current element <= the previous element
if (arr[i] <= arr[i - 1]) {
// Make the array strictly increasing
// by updating current element to
// previous element + 1;
cnt += arr[i - 1] + 1 - arr[i];
arr[i] = arr[i - 1] + 1;
}
}
return cnt;
}
static void Main() {
int[] arr = {3, 2, 1, 2, 1, 7};
Console.WriteLine(minIncrements(arr));
}
}
JavaScript
// JavaScript Program to find minimum increment operations
// to make array unique by sorting
function minIncrements(arr) {
// sort the array in increasing order
arr.sort((a, b) => a - b);
let cnt = 0;
for (let i = 1; i < arr.length; i++) {
// If current element <= the previous element
if (arr[i] <= arr[i - 1]) {
// Make the array strictly increasing
// by updating current element to
// previous element + 1;
cnt += arr[i - 1] + 1 - arr[i];
arr[i] = arr[i - 1] + 1;
}
}
return cnt;
}
// Driver Code
let arr = [3, 2, 1, 2, 1, 7];
console.log(minIncrements(arr));
Expected Approach 2 - Use Frequency Array - O(n + max) Time and O(n + max) Space
The idea is to use a frequency array to count occurrences of each number in arr[], to make all elements unique. First, we create a sufficiently large frequency array based on the maximum element and size of array. Then, we iterate over this frequency array and check if the current number's frequency is greater than 1. If it is, we increment all extra occurrences by 1 to make the current number unique. This process continues until all numbers are unique. Also we will count these increment operations during the iteration.
C++
// C++ Program to find the minimum increment operations
// needed to make the array unique by using a frequency array
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int minIncrements(vector<int>& arr) {
int n = arr.size();
int cnt = 0;
// Find the maximum element in the array
int mx = *max_element(arr.begin(), arr.end());
vector<int> freq(n + mx, 0);
// Find the frequency of all elements from the array
for (int ele : arr)
freq[ele]++;
for (int num = 0; num < freq.size(); num++) {
// If there is more than one occurrence of num
if (freq[num] > 1) {
// Increment all extra occurrences by 1
freq[num + 1] += freq[num] - 1;
// Count these increment operations
cnt += freq[num] - 1;
freq[num] = 1;
}
}
return cnt;
}
int main() {
vector<int> arr = {2, 1, 2, 4, 1};
cout << minIncrements(arr);
}
Java
// Java Program to find the minimum increment operations
// needed to make the array unique by using a frequency array
import java.util.Arrays;
class GfG {
static int minIncrements(int[] arr) {
int n = arr.length;
int cnt = 0;
// Find the maximum element in the array
int mx = arr[0];
for (int ele : arr)
mx = Math.max(mx, ele);
int[] freq = new int[n + mx];
// Find the frequency of all elements from the array
for (int ele : arr)
freq[ele]++;
for (int num = 0; num < freq.length; num++) {
// If there is more than one occurrence of num
if (freq[num] > 1) {
// Increment all extra occurrences by 1
freq[num + 1] += freq[num] - 1;
// Count these increment operations
cnt += freq[num] - 1;
freq[num] = 1;
}
}
return cnt;
}
public static void main(String[] args) {
int[] arr = {2, 1, 2, 4, 1};
System.out.println(minIncrements(arr));
}
}
Python
# Python Program to find the minimum increment operations
# needed to make the array unique by using a frequency array
def minIncrements(arr):
n = len(arr)
cnt = 0
# Find the maximum element in the array
mx = max(arr)
freq = [0] * (n + mx)
# Find the frequency of all elements from the array
for ele in arr:
freq[ele] += 1
for num in range(len(freq)):
# If there is more than one occurrence of num
if freq[num] > 1:
# Increment all extra occurrences by 1
freq[num + 1] += freq[num] - 1
# Count these increment operations
cnt += freq[num] - 1
freq[num] = 1
return cnt
if __name__ == "__main__":
arr = [2, 1, 2, 4, 1]
print(minIncrements(arr))
C#
// C# Program to find the minimum increment operations
// needed to make the array unique by using a frequency array
using System;
class GfG {
static int minIncrements(int[] arr) {
int n = arr.Length;
int cnt = 0;
// Find the maximum element in the array
int mx = arr[0];
foreach (var ele in arr) {
if (ele > mx) mx = ele;
}
int[] freq = new int[n + mx];
// Find the frequency of all elements from the array
foreach (var ele in arr) {
freq[ele]++;
}
for (int num = 0; num < freq.Length; num++) {
// If there are more than one occurrence of num
if (freq[num] > 1) {
// Increment all extra occurrences by 1
freq[num + 1] += freq[num] - 1;
// Count these increment operations
cnt += freq[num] - 1;
freq[num] = 1;
}
}
return cnt;
}
static void Main() {
int[] arr = { 2, 1, 2, 4, 1 };
Console.WriteLine(minIncrements(arr));
}
}
JavaScript
// JavaScript Program to find the minimum increment operations
// needed to make the array unique by using a frequency array
function minIncrements(arr) {
const n = arr.length;
let cnt = 0;
// Find the maximum element in the array
const mx = Math.max(...arr);
const freq = new Array(n + mx).fill(0);
// Find the frequency of all elements from the array
arr.forEach(ele => {
freq[ele]++;
});
for (let num = 0; num < freq.length; num++) {
// If there is more than one occurrence of num
if (freq[num] > 1) {
// Increment all extra occurrences by 1
freq[num + 1] += freq[num] - 1;
// Count these increment operations
cnt += freq[num] - 1;
freq[num] = 1;
}
}
return cnt;
}
// Driver Code
const arr = [2, 1, 2, 4, 1];
console.log(minIncrements(arr));
Time Complexity: O(n + max), where n is the size of the array and max is its maximum element.
Auxiliary Space: O(n + max)
Similar Reads
Minimum increment operations to make K elements equal Given an array arr[] of N elements and an integer K, the task is to make any K elements of the array equal by performing only increment operations i.e. in one operation, any element can be incremented by 1. Find the minimum number of operations required to make any K elements equal.Examples: Input:
11 min read
Minimum operation to make all elements equal in array Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Minimum operations required to make two elements equal in Array Given array A[] of size N and integer X, the task is to find the minimum number of operations to make any two elements equal in the array. In one operation choose any element A[i] and replace it with A[i] & X. where & is bitwise AND. If such operations do not exist print -1. Examples: Input:
9 min read
Minimum Bitwise OR operations to make any two array elements equal Given an array arr[] of integers and an integer K, we can perform the Bitwise OR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two eleme
9 min read
Minimum Bitwise AND operations to make any two array elements equal Given an array of integers of size 'n' and an integer 'k', We can perform the Bitwise AND operation between any array element and 'k' any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make an
10 min read
Minimum operations required to make all the array elements equal Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read
Minimum Bitwise XOR operations to make any two array elements equal Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise XOR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make a
9 min read
Minimum number of increment-other operations to make all array elements equal. We are given an array consisting of n elements. At each operation we can select any one element and increase rest of n-1 elements by 1. We have to make all elements equal performing such operation as many times you wish. Find the minimum number of operations needed for this.Examples: Input: arr[] =
5 min read
Minimum length subarray containing all unique elements after Q operations Given an array of size N containing all elements as 0 initially, and a Q queries containing range in the form of [L, R]. The task is to modify the array by adding 1 to each element in the range [L, R] for Q queries and then print the size of minimum length subarray containing all unique elements.Not
11 min read
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