Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M
Last Updated :
04 Aug, 2021
Given an array arr[] of size N consisting of only the first M natural numbers, the task is to find the minimum length of the subarray that is required to be replaced such that the frequency of array elements is N / M.
Note: N is a multiple of M.
Examples:
Input: M = 3, arr[] = {1, 1, 1, 1, 2, 3}
Output: 2
Explanation:
Replace the subarray over the range [2, 3] with the element {2, 3} modifies the array arr[] to {1, 1, 2, 3, 2, 3}. Now, the frequency of each array elements is N / M( = 6 / 3 = 2).
Therefore, the minimum length subarray that needed to be replaced is 2.
Input: M = 6, arr[] = {1, 3, 6, 6, 2, 1, 5, 4, 1, 4, 1, 2, 3, 2, 2, 2, 4, 3}
Output: 4
Approach: The given problem can be solved by using Two Pointers Approach to find the minimum length of subarray having all the count of numbers outside this range are smaller or equal to N/M. Follow the steps below to solve the problem:
- Initialize a vector, say mapu[] of size M+1 with 0 to store the frequency of each array element.
- Initialize the variable c as 0 to store the number of elements that are additionally present in the array.
- Iterate over the range [0, N] using the variable i and performing the following tasks:
- Increase the value of arr[i] in the vector mapu[] by 1.
- If the value of mapu[arr[i]] is equal to (N/M) + 1, then increase the value of c by 1.
- If the value of c is 0, then return 0 as the result.
- Initialize the variable ans as N to store the answer and L and R two pointers as 0 and (N – 1) to store the left and right of the range.
- Iterate in a while loop till R is less than N and perform the following tasks:
- If the value of (mapu[arr[R]] – 1) is equal to N/M, then subtract the value of c by 1.
- If c is equal to 0, then Iterate in a while loop till L is less than equal to R and the value of c is equal to 0 and perform the following tasks:
- Update the value of ans as the minimum of ans or (R – L + 1)
- Increase the value of mapu[arr[L]] by 1 and if that is greater than N/M, then increase the value of c by 1.
- Increase the value of L by 1.
- Increase the value of R by 1.
- After completing the above steps, print the value of ans as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimumSubarray(vector< int > arr,
int n, int m)
{
vector< int > mapu(m + 1, 0);
int c = 0;
for ( int i = 0; i < n; i++) {
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
if (c == 0)
return 0;
int ans = n;
int l = 0, r = 0;
while (r < n) {
if (--mapu[arr[r]] == (n / m))
c--;
if (c == 0) {
while (l <= r && c == 0) {
ans = min(ans, r - l + 1);
if (++mapu[arr[l]] > (n / m))
c++;
l++;
}
}
r++;
}
return ans;
}
int main()
{
vector< int > arr = { 1, 1, 2, 1, 1, 2 };
int M = 2;
int N = arr.size();
cout << minimumSubarray(arr, N, M);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
public static int minimumSubarray( int [] arr, int n,
int m)
{
int [] mapu = new int [m + 1 ];
Arrays.fill(mapu, 0 );
int c = 0 ;
for ( int i = 0 ; i < n; i++)
{
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1 )
c++;
}
if (c == 0 )
return 0 ;
int ans = n;
int l = 0 , r = 0 ;
while (r < n)
{
if (--mapu[arr[r]] == (n / m))
c--;
if (c == 0 )
{
while (l <= r && c == 0 )
{
ans = Math.min(ans, r - l + 1 );
if (++mapu[arr[l]] > (n / m))
c++;
l++;
}
}
r++;
}
return ans;
}
public static void main(String args[])
{
int [] arr = { 1 , 1 , 2 , 1 , 1 , 2 };
int M = 2 ;
int N = arr.length;
System.out.println(minimumSubarray(arr, N, M));
}
}
|
Python3
def minimumSubarray(arr, n, m):
mapu = [ 0 for i in range (m + 1 )]
c = 0
for i in range (n):
mapu[arr[i]] + = 1
if (mapu[arr[i]] = = (n / / m) + 1 ):
c + = 1
if (c = = 0 ):
return 0
ans = n
l = 0
r = 0
while (r < n):
mapu[arr[r]] - = 1
if (mapu[arr[r]] = = (n / / m)):
c - = 1
if (c = = 0 ):
while (l < = r and c = = 0 ):
ans = min (ans, r - l + 1 )
mapu[arr[l]] + = 1
if (mapu[arr[l]] > (n / / m)):
c + = 1
l + = 1
r + = 1
return ans
if __name__ = = '__main__' :
arr = [ 1 , 1 , 2 , 1 , 1 , 2 ]
M = 2
N = len (arr)
print (minimumSubarray(arr, N, M))
|
C#
using System;
class GFG{
public static int minimumSubarray( int [] arr, int n,
int m)
{
int [] mapu = new int [m + 1];
Array.Fill(mapu, 0);
int c = 0;
for ( int i = 0; i < n; i++)
{
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
if (c == 0)
return 0;
int ans = n;
int l = 0, r = 0;
while (r < n)
{
if (--mapu[arr[r]] == (n / m))
c--;
if (c == 0)
{
while (l <= r && c == 0)
{
ans = Math.Min(ans, r - l + 1);
if (++mapu[arr[l]] > (n / m))
c++;
l++;
}
}
r++;
}
return ans;
}
public static void Main(String []args)
{
int [] arr = { 1, 1, 2, 1, 1, 2 };
int M = 2;
int N = arr.Length;
Console.Write(minimumSubarray(arr, N, M));
}
}
|
Javascript
<script>
function minimumSubarray(arr, n, m)
{
let mapu = new Array(m + 1).fill(0);
let c = 0;
for (let i = 0; i < n; i++)
{
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
if (c == 0)
return 0;
let ans = n;
let l = 0, r = 0;
while (r < n)
{
if (--mapu[arr[r]] == (n / m))
c--;
if (c == 0)
{
while (l <= r && c == 0)
{
ans = Math.min(ans, r - l + 1);
if (++mapu[arr[l]] > (n / m))
c++;
l++;
}
}
r++;
}
return ans;
}
let arr = [ 1, 1, 2, 1, 1, 2 ];
let M = 2;
let N = arr.length;
document.write(minimumSubarray(arr, N, M));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...