Minimize insertions or deletions to make frequency of each array element equal to its value
Given an array arr[] of N integers, the task is to find the minimum insertion or deletion operations required to make the frequency of each element equal to its value.
Example:
Input: arr = {3, 2, 3, 1, 2}
Output: 1
Explanation: Initially, the frequency of integers in the array is freq[3] = 2, freq[2] = 2 and freq[1] = 1. In the 1st operation, insert 3 into the array. Hence the array becomes arr[] = {3, 2, 3, 1, 2, 3} having frequency of each element equal to its value.
Input: [3, 3, 4, 3, 1, 2]
Output: 2
Explanation: In 1st operation, delete 4 from the array. In 2nd operation, insert 2 into the array. Hence the array becomes arr[] = {3, 3, 3, 1, 2, 2} having frequency of each element equal to its value.
Approach: The given problem can be solved using a Greedy Approach. Follow the steps below to solve the given problem:
- Create a hash map freq, to store the frequency of all elements of the array.
- Iterate over all the values of the map using a variable key. If freq[key] > key, it will contribute (freq[key] – key) to the total operation count, otherwise, it will contribute min(freq[key], key – freq[key]) to the total operation count.
- Create a variable count which stores the operation count of all the possible key values which will be the required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int minOperations( int arr[], int n)
{
map< int , int > mp;
for ( int i = 0; i < n; i++) {
mp[arr[i]]++;
}
int count = 0;
for ( auto it : mp) {
if (it.second >= it.first)
count += (it.second - it.first);
else
count += min(it.first - it.second, it.second);
}
return count;
}
int main()
{
int arr[] = { 3, 3, 4, 3, 1, 2 };
int n = sizeof (arr) / sizeof (arr[0]);
int count = minOperations(arr, n);
cout << count;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static int minOperations( int [] arr, int n)
{
Map<Integer, Integer> freq = new HashMap<>();
for ( int val : arr)
freq.put(val, freq.getOrDefault(val, 0 ) + 1 );
int count = 0 ;
for (Map.Entry mp : freq.entrySet()) {
int key = ( int )mp.getKey();
int val = ( int )mp.getValue();
if (val >= key)
count += val - key;
else
count += Math.min(key - val, val);
}
return count;
}
public static void main(String[] args)
{
int [] arr = { 3 , 3 , 4 , 3 , 1 , 2 };
int n = arr.length;
System.out.println(minOperations(arr, n));
}
}
|
Python3
from collections import defaultdict
def minOperations(arr, n):
mp = defaultdict( int )
for i in range (n):
mp[arr[i]] + = 1
count = 0
for it in mp:
if (mp[it] > = it):
count + = (mp[it] - it)
else :
count + = min (it - mp[it], mp[it])
return count
if __name__ = = "__main__" :
arr = [ 3 , 3 , 4 , 3 , 1 , 2 ]
n = len (arr)
count = minOperations(arr, n)
print (count)
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG {
static int minOperations( int []arr, int n)
{
Dictionary< int , int > mp =
new Dictionary< int , int >();
for ( int i = 0; i < n; i++) {
if (mp.ContainsKey(arr[i])) {
int val = mp[arr[i]];
mp.Remove(arr[i]);
mp.Add(arr[i], val + 1);
}
else {
mp.Add(arr[i], 1);
}
}
int count = 0;
foreach (KeyValuePair< int , int > it in mp) {
if (it.Value >= it.Key)
count += (it.Value - it.Key);
else
count += Math.Min(it.Key - it.Value, it.Value);
}
return count;
}
public static void Main()
{
int [] arr = { 3, 3, 4, 3, 1, 2 };
int n = arr.Length;
Console.Write(minOperations(arr, n));
}
}
|
Javascript
<script>
function minOperations(arr, n)
{
var mp = new Map();
for ( var i = 0; i < n; i++)
{
if (mp.has(arr[i]))
mp.set(arr[i], mp.get(arr[i])+1)
else
mp.set(arr[i], 1)
}
var count = 0;
var keys = [];
mp.forEach((value, key) => {
keys.push(key);
});
keys.forEach((key) => {
if (mp.get(key) >= key)
count += (mp.get(key) - key);
else
count += Math.min(key - mp.get(key), mp.get(key));
});
return count;
}
var arr = [ 3, 3, 4, 3, 1, 2 ];
var n = arr.length;
var count = minOperations(arr, n);
document.write(count);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
23 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...