Open In App

Find the minimum number of elements that should be removed to make an array good

Given an array of size N and an integer K. The array consists of only digits {0, 1, 2, 3, …k-1}. The task is to make array good by removing some of the elements. The array of length x is called good if x is divisible by k and one can split the given array into x/k subsequences and each of form {0, 1, 2, 3, …k-1}.
Note: An empty array is also a good array
Examples:

Input : a[] = {0, 1, 2, 3, 4, 0, 1, 0, 1, 2, 3, 4}, K = 5 
Output :
First sequence is formed from first, second, third, fourth 
and fifth element and second sequence is formed from eighth, ninth tenth, eleventh 
and twelfth. so, remove last fifth and sixth elements.
Input : a[] = {0, 2, 1, 3}, k = 4 
Output :
Remove all elements. One can’t make subsequence of the form 
{0, 1, 2, 3}

Approach:

Below is the implementation of the above approach:




// C++ program to remove minimum elements to
// make the given array good
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove minimum elements to
// make the given array good
int MinRemove(int a[], int n, int k)
{
    // To store count of each subsequence
    vector<int> cnt(k, 0);
 
    for (int i = 0; i < n; i++) {
        // Increase the count of subsequence [0]
        if (a[i] == 0)
            cnt[0]++;
 
        // If Previous element subsequence count
        // is greater than zero then increment
        // subsequence count of current element
        // and decrement subsequence count of
        // the previous element.
        else if (cnt[a[i] - 1] > 0) {
            cnt[a[i] - 1]--;
            cnt[a[i]]++;
        }
    }
 
    // Return the required answer
    return n - (k * cnt[k - 1]);
}
 
// Driver code
int main()
{
 
    int a[] = { 0, 1, 2, 3, 4, 0,
                1, 0, 1, 2, 3, 4 },
        k = 5;
 
    int n = sizeof(a) / sizeof(a[0]);
 
    // Function call
    cout << MinRemove(a, n, k);
 
    return 0;
}




// Java program to remove minimum elements to
// make the given array good
import java.util.Collections;
import java.util.Vector;
 
class GFG
{
    // Function to remove minimum elements to
    // make the given array good
    static int MinRemove(int[] a, int n, int k)
    {
        // To store count of each subsequence
        int []cnt = new int[n];
        for (int i = 0; i < n; i++)
        {
            // Increase the count of subsequence [0]
            if (a[i] == 0)
                cnt[0]++;
 
            // If Previous element subsequence count
            // is greater than zero then increment
            // subsequence count of current element
            // and decrement subsequence count of
            // the previous element.
            else if (cnt[a[i] - 1] > 0)
            {
                cnt[a[i] - 1]--;
                cnt[a[i]]++;
            }
        }
 
        // Return the required answer
        return n - (k * cnt[k - 1]);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
 
        int a[] = { 0, 1, 2, 3, 4, 0,
                    1, 0, 1, 2, 3, 4 };
        int k = 5;
 
        int n = a.length;
 
        // Function call
        System.out.println(MinRemove(a, n, k));
    }
}
 
// This code contributed by Rajput-Ji




# Python3 program to remove minimum elements to
# make the given array good
 
# Function to remove minimum elements to
# make the given array good
def MinRemove(a, n, k) :
 
    # To store count of each subsequence
    cnt = [0] * k
 
    for i in range(n) :
        # Increase the count of subsequence [0]
        if (a[i] == 0) :
            cnt[0] += 1;
 
        # If Previous element subsequence count
        # is greater than zero then increment
        # subsequence count of current element
        # and decrement subsequence count of
        # the previous element.
        elif (cnt[a[i] - 1] > 0) :
            cnt[a[i] - 1] -= 1;
            cnt[a[i]] += 1;
 
    # Return the required answer
    return n - (k * cnt[k - 1]);
 
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 0, 1, 2, 3, 4, 0,
                1, 0, 1, 2, 3, 4 ]
    k = 5;
 
    n = len(a);
 
    # Function call
    print(MinRemove(a, n, k));
     
# This code is contributed by AnkitRai01




// C# program to remove minimum elements to
// make the given array good
using System;
 
class GFG
{
    // Function to remove minimum elements to
    // make the given array good
    static int MinRemove(int[] a, int n, int k)
    {
        // To store count of each subsequence
        int []cnt = new int[n];
        for (int i = 0; i < n; i++)
        {
            // Increase the count of subsequence [0]
            if (a[i] == 0)
                cnt[0]++;
 
            // If Previous element subsequence count
            // is greater than zero then increment
            // subsequence count of current element
            // and decrement subsequence count of
            // the previous element.
            else if (cnt[a[i] - 1] > 0)
            {
                cnt[a[i] - 1]--;
                cnt[a[i]]++;
            }
        }
 
        // Return the required answer
        return n - (k * cnt[k - 1]);
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int []a = { 0, 1, 2, 3, 4, 0,
                    1, 0, 1, 2, 3, 4 };
        int k = 5;
        int n = a.Length;
 
        // Function call
        Console.WriteLine(MinRemove(a, n, k));
    }
}
 
// This code is contributed by 29AjayKumar




<script>
 
// Javascript program to remove minimum elements to
// make the given array good
 
// Function to remove minimum elements to
// make the given array good
function MinRemove(a, n, k)
{
    // To store count of each subsequence
    let cnt = new Array(k).fill(0);
 
    for (let i = 0; i < n; i++) {
        // Increase the count of subsequence [0]
        if (a[i] == 0)
            cnt[0]++;
 
        // If Previous element subsequence count
        // is greater than zero then increment
        // subsequence count of current element
        // and decrement subsequence count of
        // the previous element.
        else if (cnt[a[i] - 1] > 0) {
            cnt[a[i] - 1]--;
            cnt[a[i]]++;
        }
    }
 
    // Return the required answer
    return n - (k * cnt[k - 1]);
}
 
// Driver code
 
    let a = [ 0, 1, 2, 3, 4, 0,
                1, 0, 1, 2, 3, 4 ],
        k = 5;
 
    let n = a.length;
 
    // Function call
    document.write(MinRemove(a, n, k));
 
</script>

Output: 
2

Time Complexity : O(N)

Auxiliary Space: O(k)
 


Article Tags :