Remove array elements to reduce frequency of each array element to at most K
Given a sorted array arr[] of size N and an integer K, the task is to print the array by removing the minimum number of array elements to make frequency of each element at most K.
Examples:
Input: arr[] = { 1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5 }, K = 3
Output: { 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5 }
Explanation:
Removing arr[0], arr[8] modifies arr[] to { 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5 }.
The frequency of each distinct array element is at most K(=3).
Therefore, the required output is { 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 5 }.Input: arr[] = { 1, 2, 2, 3, 4, 4, 4, 5, 5 }, K = 2
Output: { 1, 2, 2, 3, 4, 4, 5, 5 }
Naive Approach: The simplest approach to solve this problem is to traverse the array and check if the frequency of the array elements is greater than K or not. If found to be true, then remove the current element from the array. Otherwise, increment the frequency of the array element and print the array element.
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: Follow the steps below to solve the problem:
- Initialize a variable, say j = 0 to store the index of any array element by removing the array elements such that the frequency of each distinct element is at most K.
- Traverse the array and check if j < K or arr[i] > arr[j – K] is true or not. If found to be true, then update arr[j++] = arr[i].
- Finally, print the array by removing all the array elements whose index is greater than or equal to j.
Below is the implementation of the above approach.
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to remove array elements // such that frequency of each distinct // array element is at most K vector< int > RemoveElemArr(vector< int >& arr, int n, int k) { // Base Case if (n == 0 || n == 1) return arr; // Stores index of array element by removing // the array element such that the frequency // of array elements is at most K int j = 0; // Traverse the array for ( int i = 0; i < n; i++) { // If j < k or arr[i] > arr[j - k] if (j < k || arr[i] > arr[j - k]) { // Update arr[j] arr[j++] = arr[i]; } } // Remove array elements while (arr.size() > j) { arr.pop_back(); } return arr; } // Function to print the array void printArray(vector< int >& arr) { // Traverse the array for ( int i = 0; i < arr.size(); i++) { cout << arr[i] << " " ; } } // Utility Function to remove array elements // such that frequency of each distinct // array element is at most K void UtilRemov(vector< int >& arr, int n, int k) { arr = RemoveElemArr(arr, n, k); // Print updated array printArray(arr); } // Driver Code int main() { vector< int > arr = { 1, 2, 2, 3, 4, 4, 4, 5, 5 }; int k = 2; int n = arr.size(); UtilRemov(arr, n, k); return 0; } |
Java
// Java program for the above approach import java.util.*; class GFG { // Function to remove array elements // such that frequency of each distinct // array element is at most K static int [] RemoveElemArr( int []arr, int n, int k) { // Base Case if (n == 0 || n == 1 ) return arr; // Stores index of array element by removing // the array element such that the frequency // of array elements is at most K int j = 0 ; // Traverse the array for ( int i = 0 ; i < n; i++) { // If j < k or arr[i] > arr[j - k] if (j < k || arr[i] > arr[j - k]) { // Update arr[j] arr[j++] = arr[i]; } } // Remove array elements while (arr.length > j) { arr = Arrays.copyOfRange(arr, 0 , arr.length- 1 ); } return arr; } // Function to print the array static void printArray( int [] arr) { // Traverse the array for ( int i = 0 ; i < arr.length; i++) { System.out.print(arr[i]+ " " ); } } // Utility Function to remove array elements // such that frequency of each distinct // array element is at most K static void UtilRemov( int []arr, int n, int k) { arr = RemoveElemArr(arr, n, k); // Print updated array printArray(arr); } // Driver Code public static void main(String[] args) { int []arr = { 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 }; int k = 2 ; int n = arr.length; UtilRemov(arr, n, k); } } // This code is contributed by Amit Katiyar |
Python3
# Python3 program for the above approach # Function to remove array elements # such that frequency of each distinct # array element is at most K def RemoveElemArr(arr, n, k): # Base Case if (n = = 0 or n = = 1 ): return arr # Stores index of array element by # removing the array element such # that the frequency of array # elements is at most K j = 0 # Traverse the array for i in range (n): # If j < k or arr[i] > arr[j - k] if (j < k or arr[i] > arr[j - k]): # Update arr[j] arr[j], j = arr[i], j + 1 # Remove array elements while ( len (arr) > j): del arr[ - 1 ] return arr # Function to print the array def printArray(arr): # Traverse the array for i in arr: print (i, end = " " ) # Utility Function to remove array elements # such that frequency of each distinct # array element is at most K def UtilRemov(arr, n, k): arr = RemoveElemArr(arr, n, k) # Print updated array printArray(arr) # Driver Code if __name__ = = '__main__' : arr = [ 1 , 2 , 2 , 3 , 4 , 4 , 4 , 5 , 5 ] k = 2 n = len (arr) UtilRemov(arr, n, k) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; using System.Linq; class GFG { // Function to remove array elements // such that frequency of each distinct // array element is at most K static int [] RemoveElemArr( int []arr, int n, int k) { // Base Case if (n == 0 || n == 1) return arr; // Stores index of array element by removing // the array element such that the frequency // of array elements is at most K int j = 0; // Traverse the array for ( int i = 0; i < n; i++) { // If j < k or arr[i] > arr[j - k] if (j < k || arr[i] > arr[j - k]) { // Update arr[j] arr[j++] = arr[i]; } } // Remove array elements while (arr.Length > j) { arr = arr.Take(arr.Length - 1).ToArray(); } return arr; } // Function to print the array static void printArray( int [] arr) { // Traverse the array for ( int i = 0; i < arr.Length; i++) { Console.Write(arr[i] + " " ); } } // Utility Function to remove array elements // such that frequency of each distinct // array element is at most K static void UtilRemov( int []arr, int n, int k) { arr = RemoveElemArr(arr, n, k); // Print updated array printArray(arr); } // Driver code public static void Main() { int []arr = { 1, 2, 2, 3, 4, 4, 4, 5, 5 }; int k = 2; int n = arr.Length; UtilRemov(arr, n, k); } } // This code is contributed by sanjoy_62. |
Javascript
<script> // JavaScript program for the above approach // Function to remove array elements // such that frequency of each distinct // array element is at most K function RemoveElemArr(arr, n, k) { // Base Case if (n == 0 || n == 1) return arr; // Stores index of array element by removing // the array element such that the frequency // of array elements is at most K var j = 0; // Traverse the array for ( var i = 0; i < n; i++) { // If j < k or arr[i] > arr[j - k] if (j < k || arr[i] > arr[j - k]) { // Update arr[j] arr[j++] = arr[i]; } } // Remove array elements while (arr.length > j) { arr.pop(); } return arr; } // Function to print the array function printArray(arr) { // Traverse the array for ( var i = 0; i < arr.length; i++) { document.write(arr[i] + " " ); } } // Utility Function to remove array elements // such that frequency of each distinct // array element is at most K function UtilRemov(arr, n, k) { arr = RemoveElemArr(arr, n, k); // Print updated array printArray(arr); } var arr = [ 1, 2, 2, 3, 4, 4, 4, 5, 5 ]; var k = 2; var n = arr.length; UtilRemov(arr, n, k); // This code is contributed by SoumikMondal </script> |
1 2 2 3 4 4 5 5
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...