Related Articles
Minimum removal of K equal elements required to empty an array
• Last Updated : 24 Feb, 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 eacharray 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 a 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 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 ``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);``    ``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 cntFreq``            ``= ``new` `HashMap();` `        ``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`
Output:
`5`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up