Minimum removals required to make frequency of each array element equal to its value
Last Updated :
27 Mar, 2023
Given an array arr[] of size N, the task is to find the minimum count of array elements required to be removed such that frequency of each array element is equal to its value
Examples:
Input: arr[] = { 2, 4, 1, 4, 2 }
Output: 2
Explanation:
Removing arr[1] from the array modifies arr[] to { 2, 1, 4, 2 }
Removing arr[2] from the array modifies arr[] to { 2, 1, 2 }
Distinct elements in the array are: { 1, 2 } with frequencies 1 and 2 respectively.
Therefore, the required output is 2.
Input: arr[] = { 2, 7, 1, 8, 2, 8, 1, 8 }
Output: 5
Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:
- Initialize a map say, mp to store the frequency of each distinct element of the array.
- Traverse the array and store the frequency of each distinct element of the array.
- Initialize a variable say, cntMinRem to store the minimum count of array elements required to be removed such that the frequency of arr[i] is equal to arr[i].
- Traverse the map using key value of the map as i and check the following conditions:
- If mp[i] < i, then update the value of cntMinRem += mp[i].
- If mp[i] > i, then update the value of cntMinRem += (mp[i] – i).
- Finally, print the value of cntMinRem.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
int min_elements( int arr[], int N)
{
unordered_map< int , int > mp;
for ( int i = 0; i < N; i++) {
mp[arr[i]]++;
}
int cntMinRem = 0;
for ( auto it : mp) {
int i = it.first;
if (mp[i] < i) {
cntMinRem += mp[i];
}
else if (mp[i] > i) {
cntMinRem += (mp[i] - i);
}
}
return cntMinRem;
}
int main()
{
int arr[] = { 2, 4, 1, 4, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << min_elements(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
public static int min_elements( int arr[], int N)
{
Map<Integer,
Integer> mp = new HashMap<Integer,
Integer>();
for ( int i = 0 ; i < N; i++)
{
mp.put(arr[i],
mp.getOrDefault(arr[i], 0 ) + 1 );
}
int cntMinRem = 0 ;
for ( int key : mp.keySet())
{
int i = key;
int val = mp.get(i);
if (val < i)
{
cntMinRem += val;
}
else if (val > i)
{
cntMinRem += (val - i);
}
}
return cntMinRem;
}
public static void main(String[] args)
{
int arr[] = { 2 , 4 , 1 , 4 , 2 };
System.out.println(min_elements(
arr, arr.length));
}
}
|
Python3
def min_elements(arr, N) :
mp = {};
for i in range (N) :
if arr[i] in mp :
mp[arr[i]] + = 1 ;
else :
mp[arr[i]] = 1 ;
cntMinRem = 0 ;
for it in mp :
i = it;
if (mp[i] < i) :
cntMinRem + = mp[i];
elif (mp[i] > i) :
cntMinRem + = (mp[i] - i);
return cntMinRem;
if __name__ = = "__main__" :
arr = [ 2 , 4 , 1 , 4 , 2 ];
N = len (arr);
print (min_elements(arr, N));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int min_elements( int [] arr, int N)
{
Dictionary< int , int > mp = new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
if (mp.ContainsKey(arr[i]))
{
mp[arr[i]]++;
}
else
{
mp[arr[i]] = 1;
}
}
int cntMinRem = 0;
foreach (KeyValuePair< int , int > it in mp)
{
int i = it.Key;
if (mp[i] < i)
{
cntMinRem += mp[i];
}
else if (mp[i] > i)
{
cntMinRem += (mp[i] - i);
}
}
return cntMinRem;
}
static void Main()
{
int [] arr = { 2, 4, 1, 4, 2 };
int N = arr.Length;
Console.Write(min_elements(arr, N));
}
}
|
Javascript
<script>
function min_elements(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 cntMinRem = 0;
mp.forEach((value, key) => {
var i = key;
if (mp.get(i) < i) {
cntMinRem += mp.get(i);
}
else if (mp.get(i) > i) {
cntMinRem += (mp.get(i) - i);
}
});
return cntMinRem;
}
var arr = [2, 4, 1, 4, 2];
var N = arr.length;
document.write( min_elements(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 2: No Extra Space:
The above approach uses a unordered_map and has auxiliary space O(N).
- To optimize the given code in O(1) space, we can make use of the fact that the input array elements are positive integers. We can make use of the input array itself to store the frequency of each element.
- We can iterate over the array and for each element arr[i], we can increment the frequency of element (arr[i] % N) by N. Here, N is the size of the input array. This will not change the value of the element and will help us keep track of the frequency of the element in the array.
- After we have updated the frequency of each element, we can iterate over the array again and count the number of elements for which the frequency is less than or greater than the element value. We can return the sum of these counts as the minimum count of elements required to be removed.
Here is the optimized code:
C++
#include <iostream>
using namespace std;
int min_elements( int arr[], int N) {
for ( int i = 0; i < N; i++) {
arr[arr[i] % N] += N;
}
int cntMinRem = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] / N < i) {
cntMinRem += arr[i] / N;
}
if (arr[i] / N > i + 1) {
cntMinRem += (arr[i] / N - i - 1);
}
}
return cntMinRem;
}
int main() {
int arr[] = { 2, 4, 1, 4, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << min_elements(arr, N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int minElements( int [] arr, int N) {
for ( int i = 0 ; i < N; i++) {
arr[arr[i] % N] += N;
}
int cntMinRem = 0 ;
for ( int i = 0 ; i < N; i++) {
if (arr[i] / N < i) {
cntMinRem += arr[i] / N;
}
if (arr[i] / N > i + 1 ) {
cntMinRem += (arr[i] / N - i - 1 );
}
}
return cntMinRem;
}
public static void main(String[] args) {
int [] arr = { 2 , 4 , 1 , 4 , 2 };
int N = arr.length;
System.out.println(minElements(arr, N));
}
}
|
Python3
def min_elements(arr, N):
for i in range (N):
arr[arr[i] % N] + = N
cntMinRem = 0
for i in range (N):
if arr[i] / / N < i:
cntMinRem + = arr[i] / / N
if arr[i] / / N > i + 1 :
cntMinRem + = arr[i] / / N - i - 1
return cntMinRem
arr = [ 2 , 4 , 1 , 4 , 2 ]
N = len (arr)
print (min_elements(arr, N))
|
C#
using System;
public class Program {
public static int MinElements( int [] arr, int N)
{
for ( int i = 0; i < N; i++) {
arr[arr[i] % N] += N;
}
int cntMinRem = 0;
for ( int i = 0; i < N; i++) {
if (arr[i] / N < i) {
cntMinRem += arr[i] / N;
}
if (arr[i] / N > i + 1) {
cntMinRem += (arr[i] / N - i - 1);
}
}
return cntMinRem;
}
public static void Main()
{
int [] arr = { 2, 4, 1, 4, 2 };
int N = arr.Length;
Console.WriteLine(MinElements(arr, N));
}
}
|
Javascript
function min_elements(arr, N) {
for (let i = 0; i < N; i++) {
arr[arr[i] % N] += N;
}
let cntMinRem = 0;
for (let i = 0; i < N; i++) {
if (arr[i] / N < i) {
cntMinRem += Math.floor(arr[i] / N);
}
if (arr[i] / N > i + 1) {
cntMinRem += Math.floor(arr[i] / N) - i - 1;
}
}
return cntMinRem;
}
let arr = [2, 4, 1, 4, 2];
let N = arr.length;
console.log(min_elements(arr, N));
|
OUTPUT:
2
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...