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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
void minSteps(int arr[], int N, int K)
{
// Stores the minimum number of
// steps required to empty the array
int count = 0;
// Stores the occurrence
// of each array element
map<int, int> cntFreq;
for (int i = 0; i < N; i++)
{
// Update the frequency
cntFreq[arr[i]]++;
}
// Traverse the Hashmap
for (auto i : cntFreq)
{
// Check if the frequency
// is divisible by K or not
if (i.first % K == 0)
count += i.second / K;
// Otherwise
else
count += (i.second / K) + 1;
}
// Print the count of
// minimum steps required
cout << (count);
}
// Driver Code
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;
}
// This code is contributed by Dharanendra L V.
Java
// Java program for the above approach
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class GFG {
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
public static void minSteps(
int[] arr, int N, int K)
{
// Stores the minimum number of
// steps required to empty the array
int count = 0;
// Stores the occurrence
// of each array element
Map<Integer, Integer> cntFreq
= new HashMap<Integer, Integer>();
for (int i = 0; i < N; i++) {
// Update the frequency
cntFreq.put(
arr[i],
cntFreq.getOrDefault(
arr[i], 0)
+ 1);
}
// Traverse the Hashmap
for (Integer i : cntFreq.keySet()) {
// Check if the frequency
// is divisible by K or not
if (cntFreq.get(i) % K == 0)
count += cntFreq.get(i)
/ K;
// Otherwise
else
count += (cntFreq.get(i)
/ K)
+ 1;
}
// Print the count of
// minimum steps required
System.out.print(count);
}
// Driver Code
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
# Python3 program for the above approach
# Function to count the minimum
# number of steps required to empty
# given array by removing at most K
# equal array elements in each operation
def minSteps(arr, N, K) :
# Stores the minimum number of
# steps required to empty the array
count = 0
# Stores the occurrence
# of each array element
cntFreq = {}
for i in range(N) :
# Update the frequency
if arr[i] in cntFreq :
cntFreq[arr[i]] += 1
else :
cntFreq[arr[i]] = 1
# Traverse the Hashmap
for i in cntFreq :
# Check if the frequency
# is divisible by K or not
if (i % K == 0) :
count += cntFreq[i] // K
# Otherwise
else :
count += (cntFreq[i] // K) + 1
# Print the count of
# minimum steps required
print(count)
arr = [ 4, 4, 7, 3, 1, 1, 2, 1, 7, 3 ]
N = len(arr)
K = 5
minSteps(arr, N, K)
# This code is contributed by divyeshabadiya07.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
public static void minSteps(
int[] arr, int N, int K)
{
// Stores the minimum number of
// steps required to empty the array
int count = 0;
// Stores the occurrence
// of each array element
Dictionary<int, int> cntFreq
= new Dictionary<int, int>();
for (int i = 0; i < N; i++) {
// Update the frequency
if(cntFreq.ContainsKey(arr[i]))
cntFreq[arr[i]] = cntFreq[arr[i]]+1;
else
cntFreq.Add(arr[i],1);
}
// Traverse the Hashmap
foreach (int i in cntFreq.Keys) {
// Check if the frequency
// is divisible by K or not
if (cntFreq[i] % K == 0)
count += cntFreq[i]
/ K;
// Otherwise
else
count += (cntFreq[i]
/ K)
+ 1;
}
// Print the count of
// minimum steps required
Console.Write(count);
}
// Driver Code
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);
}
}
// This code is contributed by shikhasingrajput
JavaScript
<script>
// JavaScript program for the above approach
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
function minSteps(arr, N, K) {
// Stores the minimum number of
// steps required to empty the array
var count = 0;
// Stores the occurrence
// of each array element
var cntFreq = {};
for (var i = 0; i < N; i++) {
// Update the frequency
if (cntFreq.hasOwnProperty(arr[i]))
cntFreq[arr[i]] += 1;
else
cntFreq[arr[i]] = 1;
}
// Traverse the Hashmap
for (const [key, value] of Object.entries(cntFreq)) {
// Check if the frequency
// is divisible by K or not
if (key % K == 0)
count += parseInt(cntFreq[key] / K);
// Otherwise
else
count += parseInt(cntFreq[key] / K) + 1;
}
// Print the count of
// minimum steps required
document.write(count);
}
// Driver Code
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)
Similar Reads
Minimum removal of elements from end of an array required to obtain sum K Given an integer K and an array A[] of size N, the task is to create a new array with sum K with minimum number of operations, where in each operation, an element can be removed either from the start or end of A[] and appended to the new array. If it is not possible to generate a new array with sum
15+ min read
Minimum operations required to make all the array elements equal Given an array arr[] of n integer and an integer k. The task is to count the minimum number of times the given operation is required to make all the array elements equal. In a single operation, the kth element of the array is appended at the end of the array and the first element of the array gets d
6 min read
Minimum increment and decrement by K of each pair elements required to make all array elements equal Given an array arr[], the task is to check if it is possible to make all array elements equal by repeatedly choosing a triplet (i, j, k), where i and j are different, and subtract k from arr[i] and add k to arr[j]. Examples: Input: arr[] = {1, 5, 6, 4}Output: YesExplanation:Operations performed: Cho
10 min read
Minimum Bitwise OR operations to make any two array elements equal Given an array arr[] of integers and an integer K, we can perform the Bitwise OR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two eleme
9 min read
Minimum operations of given type required to empty given array Given an array arr[] of size N, the task is to find the total count of operations required to remove all the array elements such that if the first element of the array is the smallest element, then remove that element, otherwise move the first element to the end of the array. Examples: Input: A[] =
14 min read