![]() |
VOOZH | about |
Given an array arr[] of length N and a positive integer k, the task is to find the minimum length of the subarray that needs to be removed from the given array such that the frequency of the remaining elements in the array is less than or equal to k.
Examples:
Input: n = 4, arr[] = {3, 1, 3, 6}, k = 1
Output: 1
Explanation: We can see that only 3 is having frequency 2 that is greater than k. So we can remove the 3 at the start of the array to that frequency of all elements become less than or equal to k. Thus the minimum length of the subarray to be removed is 1.Input: n = 6, arr[] = {1, 2, 3, 3, 2, 1}, k = 1
Output: 3
Explanation: We Can remove the subarray {1, 2, 3} which is the minimum possible length subarray after being removed making the frequency of remaining elements less than or equal to k.
Approach: This can be solved with the following idea:
This problem can be solved usingBinary Search and Sliding Window Technique.
Below are the steps involved in the implementation of the code:
Below is the implementation of the above approach:
1
Time Complexity: O(N*logN)
Auxiliary Space: O(N)