Minimum removal of K equal elements required to empty an array
Last Updated :
31 May, 2021
Given an array arr[] consisting of N integers, the task is to count the minimum number of times at most K equal elements are required to be removed to make the array empty.
Examples:
Input: arr[] = {1, 3, 1, 1, 3}, K = 2
Output: 3
Explanation:
Step 1: Remove at most 2 1s from the array. The modified array is {1, 3, 3}.
Step 2: Remove at most 2 3s from the array. The modified array is {1}.
Step 3: Remove at most 2 1s from the array. The modified array is {}.
After 3 steps, the array becomes empty.
Therefore, the minimum number of steps required is 3.
Input: arr[] = {4, 4, 7, 3, 1, 1, 2, 1, 7, 3}, K = 5
Output: 5
Naive Approach: The simplest approach is to traverse the array and count the frequency of every array element and then, divide the frequency of every element by K and add it to count. Increment count if the frequency of the array element is not divisible by K. After completing the above steps, print the value of count as the result.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by Hashing to store the frequency of each array element and then count the minimum number of operations required. Follow the steps below to solve the problem:
- Initialize a variable, say, count, that stores the minimum number of steps required.
- Initialize Hashmap that stores the frequency of each element in the array.
- Traverse the array arr[] and store the frequencies of each element in the Hashmap.
- Traverse the Hashmap and add the value of frequency of each element, divided by K, to the variable count. If the frequency of the current array element is not divisible by K, then increment the count by 1.
- After completing the above steps, print count as the required minimum number of steps required to make the array empty.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minSteps( int arr[], int N, int K)
{
int count = 0;
map< int , int > cntFreq;
for ( int i = 0; i < N; i++)
{
cntFreq[arr[i]]++;
}
for ( auto i : cntFreq)
{
if (i.first % K == 0)
count += i.second / K;
else
count += (i.second / K) + 1;
}
cout << (count);
}
int main()
{
int arr[] = { 4, 4, 7, 3, 1,
1, 2, 1, 7, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
int K = 5;
minSteps(arr, N, K);
return 0;
}
|
Java
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class GFG {
public static void minSteps(
int [] arr, int N, int K)
{
int count = 0 ;
Map<Integer, Integer> cntFreq
= new HashMap<Integer, Integer>();
for ( int i = 0 ; i < N; i++) {
cntFreq.put(
arr[i],
cntFreq.getOrDefault(
arr[i], 0 )
+ 1 );
}
for (Integer i : cntFreq.keySet()) {
if (cntFreq.get(i) % K == 0 )
count += cntFreq.get(i)
/ K;
else
count += (cntFreq.get(i)
/ K)
+ 1 ;
}
System.out.print(count);
}
public static void main(String[] args)
{
int arr[] = { 4 , 4 , 7 , 3 , 1 ,
1 , 2 , 1 , 7 , 3 };
int N = arr.length;
int K = 5 ;
minSteps(arr, N, K);
}
}
|
Python3
def minSteps(arr, N, K) :
count = 0
cntFreq = {}
for i in range (N) :
if arr[i] in cntFreq :
cntFreq[arr[i]] + = 1
else :
cntFreq[arr[i]] = 1
for i in cntFreq :
if (i % K = = 0 ) :
count + = cntFreq[i] / / K
else :
count + = (cntFreq[i] / / K) + 1
print (count)
arr = [ 4 , 4 , 7 , 3 , 1 , 1 , 2 , 1 , 7 , 3 ]
N = len (arr)
K = 5
minSteps(arr, N, K)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void minSteps(
int [] arr, int N, int K)
{
int count = 0;
Dictionary< int , int > cntFreq
= new Dictionary< int , int >();
for ( int i = 0; i < N; i++) {
if (cntFreq.ContainsKey(arr[i]))
cntFreq[arr[i]] = cntFreq[arr[i]]+1;
else
cntFreq.Add(arr[i],1);
}
foreach ( int i in cntFreq.Keys) {
if (cntFreq[i] % K == 0)
count += cntFreq[i]
/ K;
else
count += (cntFreq[i]
/ K)
+ 1;
}
Console.Write(count);
}
public static void Main(String[] args)
{
int []arr = { 4, 4, 7, 3, 1,
1, 2, 1, 7, 3 };
int N = arr.Length;
int K = 5;
minSteps(arr, N, K);
}
}
|
Javascript
<script>
function minSteps(arr, N, K) {
var count = 0;
var cntFreq = {};
for ( var i = 0; i < N; i++) {
if (cntFreq.hasOwnProperty(arr[i]))
cntFreq[arr[i]] += 1;
else
cntFreq[arr[i]] = 1;
}
for (const [key, value] of Object.entries(cntFreq)) {
if (key % K == 0)
count += parseInt(cntFreq[key] / K);
else
count += parseInt(cntFreq[key] / K) + 1;
}
document.write(count);
}
var arr = [4, 4, 7, 3, 1, 1, 2, 1, 7, 3];
var N = arr.length;
var K = 5;
minSteps(arr, N, K);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...