Minimum deletions required such that any number X will occur exactly X times
Given an array arr[] of N integers, the task is to find the minimum deletions required such that frequency of arr[i] is exactly arr[i] in the array for all possible values of i.
Examples:
Input: arr[] = {1, 2, 2, 3, 3}
Output: 2
Frequency(1) = 1
Frequency(2) = 2
Frequency(3) = 2, frequency can’t be increased
So, delete every occurrence of 3.
Input: arr[] = {2, 3, 2, 3, 4, 4, 4, 4, 5}
Output: 3
Approach: There are two cases:
- If frequency of X is greater than or equal o X then we delete extra frequencies of X to get exactly X elements of value X.
- If frequency of X is less than X then we delete all of the occurrences of X as it is impossible to get extra element of value X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinDeletion( int a[], int n)
{
unordered_map< int , int > map;
for ( int i = 0; i < n; i++)
map[a[i]]++;
int ans = 0;
for ( auto i : map) {
int x = i.first;
int frequency = i.second;
if (x <= frequency) {
ans += (frequency - x);
}
else
ans += frequency;
}
return ans;
}
int main()
{
int a[] = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
int n = sizeof (a) / sizeof (a[0]);
cout << MinDeletion(a, n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int MinDeletion( int a[], int n)
{
Map<Integer,Integer> mp = new HashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (mp.containsKey(a[i]))
{
mp.put(a[i], mp.get(a[i])+ 1 );
}
else
{
mp.put(a[i], 1 );
}
}
int ans = 0 ;
for (Map.Entry<Integer,Integer> i : mp.entrySet())
{
int x = i.getKey();
int frequency = i.getValue();
if (x <= frequency)
{
ans += (frequency - x);
}
else
ans += frequency;
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 };
int n = a.length;
System.out.println(MinDeletion(a, n));
}
}
|
Python3
def MinDeletion(a, n) :
map = dict .fromkeys(a, 0 );
for i in range (n) :
map [a[i]] + = 1 ;
ans = 0 ;
for key,value in map .items() :
x = key;
frequency = value;
if (x < = frequency) :
ans + = (frequency - x);
else :
ans + = frequency;
return ans;
if __name__ = = "__main__" :
a = [ 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 ];
n = len (a);
print (MinDeletion(a, n));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int MinDeletion( 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 ans = 0;
foreach (KeyValuePair< int , int > i in mp)
{
int x = i.Key;
int frequency = i.Value;
if (x <= frequency)
{
ans += (frequency - x);
}
else
ans += frequency;
}
return ans;
}
public static void Main(String[] args)
{
int []a = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
int n = a.Length;
Console.WriteLine(MinDeletion(a, n));
}
}
|
Javascript
<script>
function MinDeletion( a, n){
let map = new Map();
for (let i = 0; i < n; i++){
if (map[a[i]])
map[a[i]]++;
else
map[a[i]] = 1
}
let ans = 0;
for ( var m in map){
let x = m;
let frequency = map[m];
if (x <= frequency) {
ans += (frequency - x);
}
else
ans += frequency;
};
return ans;
}
let a = [ 2, 3, 2, 3, 4, 4, 4, 4, 5 ];
let n = a.length;
document.write( MinDeletion(a, n));
</script>
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(n), where n is the size of the given array.
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...