Find the minimum number of elements that should be removed to make an array good
Last Updated :
31 May, 2022
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++
// 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
// 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
# 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#
// 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
JavaScript
<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>
Time Complexity : O(N)
Auxiliary Space: O(k)
Similar Reads
Minimum number of elements that should be removed to make the array good Given an array arr[], the task is to find the minimum number of elements that must be removed to make the array good. A sequence a1, a2 ... an is called good if for each element ai, there exists an element aj (i not equals to j) such that ai + aj is a power of two i.e. 2d for some non-negative integ
13 min read
Minimum prime numbers required to be subtracted to make all array elements equal Given an array arr[] consisting of N positive integers, the task is to find the minimum number of primes numbers required to be subtracted from the array elements to make all array elements equal. Examples: Input: arr[]= {7, 10, 4, 5}Output: 5Explanation: Following subtraction of primes numbers make
12 min read
Remove minimum elements from the array such that 2*min becomes more than max Given an unsorted array, arr[]. The task is to find the minimum number of removals required such that twice the minimum element in the array is greater than or equal to the maximum in the array.Examples: Input: arr[] = [4, 5, 100, 9, 10, 11, 12, 15, 200]Output: 4Explanation: In the given array 4 ele
7 min read
Find the minimum operations required to make Array elements Divisible by 3 mathGiven an array A[] of size N, count the minimum number of operations required such that all the elements of the array are divisible by 3. In one operation, we can take any two elements from the array, remove them, and append their sum at the end of the array. Note: If we can't make all elements
6 min read
Remove minimum elements from ends of array so that sum decreases by at least K | O(N) Given an array arr[] consisting of N elements, the task is to remove minimum number of elements from the ends of the array such that the total sum of the array decreases by at least K. Note that K will always be less than or equal to the sum of all the elements of the array. Examples: Input: arr[] =
7 min read
Find the minimum value of K required to remove all elements from the array in at most M operations Given array A[] of size N and integer M, Perform the following operation until the array becomes empty. Choose K and the pick first K elements of array A[]. In one operation subtract all these chosen K elements by 1 if any element becomes zero that element will be deleted and it will be replaced by
13 min read