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 : 2
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 : 4
Remove all elements. One can’t make subsequence of the form
{0, 1, 2, 3}
Approach:
- Let cnt0 be the number of subsequences of [0], cnt1 be the number of subsequences [0, 1], cnt2 — the number of subsequences [0, 1, 2] and so on, and cntk-1 is the number of completed subsequences [0, 1, 2, 3, …k-1].
- Iterate over all elements of arr in order from left to right.If the current element in the array is zero then increase the count of cnt0 by 1.
- If the current element in the array is not zero then check if its previous element in the sequence count is greater than zero or not.
- If its previous element in the sequence is greater than zero then decrement the count of the previous element by one and increment count of the current element by one.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinRemove( int a[], int n, int k)
{
vector< int > cnt(k, 0);
for ( int i = 0; i < n; i++) {
if (a[i] == 0)
cnt[0]++;
else if (cnt[a[i] - 1] > 0) {
cnt[a[i] - 1]--;
cnt[a[i]]++;
}
}
return n - (k * cnt[k - 1]);
}
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]);
cout << MinRemove(a, n, k);
return 0;
}
|
Java
import java.util.Collections;
import java.util.Vector;
class GFG
{
static int MinRemove( int [] a, int n, int k)
{
int []cnt = new int [n];
for ( int i = 0 ; i < n; i++)
{
if (a[i] == 0 )
cnt[ 0 ]++;
else if (cnt[a[i] - 1 ] > 0 )
{
cnt[a[i] - 1 ]--;
cnt[a[i]]++;
}
}
return n - (k * cnt[k - 1 ]);
}
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;
System.out.println(MinRemove(a, n, k));
}
}
|
Python3
def MinRemove(a, n, k) :
cnt = [ 0 ] * k
for i in range (n) :
if (a[i] = = 0 ) :
cnt[ 0 ] + = 1 ;
elif (cnt[a[i] - 1 ] > 0 ) :
cnt[a[i] - 1 ] - = 1 ;
cnt[a[i]] + = 1 ;
return n - (k * cnt[k - 1 ]);
if __name__ = = "__main__" :
a = [ 0 , 1 , 2 , 3 , 4 , 0 ,
1 , 0 , 1 , 2 , 3 , 4 ]
k = 5 ;
n = len (a);
print (MinRemove(a, n, k));
|
C#
using System;
class GFG
{
static int MinRemove( int [] a, int n, int k)
{
int []cnt = new int [n];
for ( int i = 0; i < n; i++)
{
if (a[i] == 0)
cnt[0]++;
else if (cnt[a[i] - 1] > 0)
{
cnt[a[i] - 1]--;
cnt[a[i]]++;
}
}
return n - (k * cnt[k - 1]);
}
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;
Console.WriteLine(MinRemove(a, n, k));
}
}
|
Javascript
<script>
function MinRemove(a, n, k)
{
let cnt = new Array(k).fill(0);
for (let i = 0; i < n; i++) {
if (a[i] == 0)
cnt[0]++;
else if (cnt[a[i] - 1] > 0) {
cnt[a[i] - 1]--;
cnt[a[i]]++;
}
}
return n - (k * cnt[k - 1]);
}
let a = [ 0, 1, 2, 3, 4, 0,
1, 0, 1, 2, 3, 4 ],
k = 5;
let n = a.length;
document.write(MinRemove(a, n, k));
</script>
|
Time Complexity : O(N)
Auxiliary Space: O(k)
Last Updated :
31 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...