Minimum operations required to make all the elements distinct in an array
Given an array of N integers. If a number occurs more than once, choose any number y from the array and replace the x in the array to x+y such that x+y is not in the array. The task is to find the minimum number of operations to make the array a distinct one.
Examples:
Input: a[] = {2, 1, 2}
Output: 1
Let x = 2, y = 1 then replace 2 by 3.
Performing the above step makes all the elements in the array distinct.
Input: a[] = {1, 2, 3}
Output: 0
Approach: If a number appears more than once, then the summation of (occurrences-1) for all duplicate elements will be the answer. The main logic behind this is if x is replaced by x+y where y is the largest element in the array, then x is replaced by x+y which is the largest element in the array. Use a map to store the frequency of the numbers of array. Traverse in the map, and if the frequency of an element is more than 1, add it to the count by subtracting one.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumOperations( int a[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++)
mp[a[i]] += 1;
int count = 0;
for ( auto it = mp.begin(); it != mp.end(); it++) {
if ((*it).second > 1)
count += (*it).second-1;
}
return count;
}
int main()
{
int a[] = { 2, 1, 2, 3, 3, 4, 3 };
int n = sizeof (a) / sizeof (a[0]);
cout << minimumOperations(a, n);
return 0;
}
|
Java
import java.util.*;
class geeks
{
public static int minimumOperations( int [] a, int n)
{
HashMap<Integer, Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (mp.get(a[i]) != null )
{
int x = mp.get(a[i]);
mp.put(a[i], ++x);
}
else
mp.put(a[i], 1 );
}
int count = 0 ;
for (HashMap.Entry<Integer, Integer> entry : mp.entrySet())
{
if (entry.getValue() > 1 )
{
count += (entry.getValue() - 1 );
}
}
return count;
}
public static void main(String[] args)
{
int [] a = { 2 , 1 , 2 , 3 , 3 , 4 , 3 };
int n = a.length;
System.out.println(minimumOperations(a, n));
}
}
|
Python3
def minimumOperations(a, n):
mp = dict ()
for i in range (n):
if a[i] in mp.keys():
mp[a[i]] + = 1
else :
mp[a[i]] = 1
count = 0
for it in mp:
if (mp[it] > 1 ):
count + = mp[it] - 1
return count
a = [ 2 , 1 , 2 , 3 , 3 , 4 , 3 ]
n = len (a)
print (minimumOperations(a, n))
|
C#
using System;
using System.Collections.Generic;
class geeks
{
public static int minimumOperations( int [] a, int n)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0 ; i < n; i++)
{
if (mp.ContainsKey(a[i]))
{
var val = mp[a[i]];
mp.Remove(a[i]);
mp.Add(a[i], val + 1);
}
else
{
mp.Add(a[i], 1);
}
}
int count = 0;
foreach (KeyValuePair< int , int > entry in mp)
{
if (entry.Value > 1)
{
count += (entry.Value - 1);
}
}
return count;
}
public static void Main(String[] args)
{
int [] a = { 2, 1, 2, 3, 3, 4, 3 };
int n = a.Length;
Console.WriteLine(minimumOperations(a, n));
}
}
|
Javascript
<script>
function minimumOperations(a,n)
{
let mp = new Map();
for (let i = 0; i < n; i++)
{
if (mp.get(a[i]) != null )
{
let x = mp.get(a[i]);
mp.set(a[i], ++x);
}
else
mp.set(a[i], 1);
}
let count = 0;
for (let [key, value] of mp.entries())
{
if (value > 1)
{
count += (value - 1);
}
}
return count;
}
let a=[2, 1, 2, 3, 3, 4, 3];
let n = a.length;
document.write(minimumOperations(a, n));
</script>
|
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(N), where N represents the size of the given array.
Last Updated :
23 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...