The Wayback Machine - https://web.archive.org/web/20210213080148/https://www.geeksforgeeks.org/minimum-removal-of-k-equal-elements-required-to-empty-an-array/
Skip to content
Related Articles

Related Articles

Minimum removal of K equal elements required to empty an array
  • Last Updated : 12 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:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

5

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :