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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the minimum length
// of the subarray to be changed.
int minimumSubarray(vector<int> arr,
int n, int m)
{
// Stores the frequencies of array
// elements
vector<int> mapu(m + 1, 0);
// Stores the number of array elements
// that are present more than N/M times
int c = 0;
// Iterate over the range
for (int i = 0; i < n; i++) {
// Increment the frequency
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
// If the frequency of all array
// elements are already N/M
if (c == 0)
return 0;
// Stores the resultant length of
// the subarray
int ans = n;
// The left and right pointers
int l = 0, r = 0;
// Iterate over the range
while (r < n) {
// If the current element is
if (--mapu[arr[r]] == (n / m))
c--;
// If the value of c is 0, then
// find the possible answer
if (c == 0) {
// Iterate over the range
while (l <= r && c == 0) {
ans = min(ans, r - l + 1);
// If the element at left
// is making it extra
if (++mapu[arr[l]] > (n / m))
c++;
// Update the left pointer
l++;
}
}
// Update the right pointer
r++;
}
// Return the resultant length
return ans;
}
// Driver Code
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
// Java program for the above approach
import java.util.Arrays;
class GFG{
// Function to find the minimum length
// of the subarray to be changed.
public static int minimumSubarray(int[] arr, int n,
int m)
{
// Stores the frequencies of array
// elements
int[] mapu = new int[m + 1];
Arrays.fill(mapu, 0);
// Stores the number of array elements
// that are present more than N/M times
int c = 0;
// Iterate over the range
for(int i = 0; i < n; i++)
{
// Increment the frequency
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
// If the frequency of all array
// elements are already N/M
if (c == 0)
return 0;
// Stores the resultant length of
// the subarray
int ans = n;
// The left and right pointers
int l = 0, r = 0;
// Iterate over the range
while (r < n)
{
// If the current element is
if (--mapu[arr[r]] == (n / m))
c--;
// If the value of c is 0, then
// find the possible answer
if (c == 0)
{
// Iterate over the range
while (l <= r && c == 0)
{
ans = Math.min(ans, r - l + 1);
// If the element at left
// is making it extra
if (++mapu[arr[l]] > (n / m))
c++;
// Update the left pointer
l++;
}
}
// Update the right pointer
r++;
}
// Return the resultant length
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by gfgking
Python3
# Python3 program for the above approach
# Function to find the minimum length
# of the subarray to be changed.
def minimumSubarray(arr, n, m):
# Stores the frequencies of array
# elements
mapu = [0 for i in range(m+1)]
# Stores the number of array elements
# that are present more than N/M times
c = 0
# Iterate over the range
for i in range(n):
# Increment the frequency
mapu[arr[i]] += 1
if (mapu[arr[i]] == (n // m) + 1):
c += 1
# If the frequency of all array
# elements are already N/M
if (c == 0):
return 0
# Stores the resultant length of
# the subarray
ans = n
# The left and right pointers
l = 0
r = 0
# Iterate over the range
while (r < n):
# If the current element is
mapu[arr[r]] -= 1
if (mapu[arr[r]] == (n // m)):
c -= 1
# If the value of c is 0, then
# find the possible answer
if (c == 0):
# Iterate over the range
while (l <= r and c == 0):
ans = min(ans, r - l + 1)
# If the element at left
# is making it extra
mapu[arr[l]] += 1
if (mapu[arr[l]] > (n // m)):
c += 1
# Update the left pointer
l += 1
# Update the right pointer
r += 1
# Return the resultant length
return ans
# Driver Code
if __name__ == '__main__':
arr = [1, 1, 2, 1, 1, 2]
M = 2
N = len(arr)
print(minimumSubarray(arr, N, M))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum length
// of the subarray to be changed.
public static int minimumSubarray(int[] arr, int n,
int m)
{
// Stores the frequencies of array
// elements
int[] mapu = new int[m + 1];
Array.Fill(mapu, 0);
// Stores the number of array elements
// that are present more than N/M times
int c = 0;
// Iterate over the range
for(int i = 0; i < n; i++)
{
// Increment the frequency
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
// If the frequency of all array
// elements are already N/M
if (c == 0)
return 0;
// Stores the resultant length of
// the subarray
int ans = n;
// The left and right pointers
int l = 0, r = 0;
// Iterate over the range
while (r < n)
{
// If the current element is
if (--mapu[arr[r]] == (n / m))
c--;
// If the value of c is 0, then
// find the possible answer
if (c == 0)
{
// Iterate over the range
while (l <= r && c == 0)
{
ans = Math.Min(ans, r - l + 1);
// If the element at left
// is making it extra
if (++mapu[arr[l]] > (n / m))
c++;
// Update the left pointer
l++;
}
}
// Update the right pointer
r++;
}
// Return the resultant length
return ans;
}
// Driver Code
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));
}
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
// JavaScript program for the above approach
// Function to find the minimum length
// of the subarray to be changed.
function minimumSubarray(arr, n, m)
{
// Stores the frequencies of array
// elements
let mapu = new Array(m + 1).fill(0);
// Stores the number of array elements
// that are present more than N/M times
let c = 0;
// Iterate over the range
for(let i = 0; i < n; i++)
{
// Increment the frequency
mapu[arr[i]]++;
if (mapu[arr[i]] == (n / m) + 1)
c++;
}
// If the frequency of all array
// elements are already N/M
if (c == 0)
return 0;
// Stores the resultant length of
// the subarray
let ans = n;
// The left and right pointers
let l = 0, r = 0;
// Iterate over the range
while (r < n)
{
// If the current element is
if (--mapu[arr[r]] == (n / m))
c--;
// If the value of c is 0, then
// find the possible answer
if (c == 0)
{
// Iterate over the range
while (l <= r && c == 0)
{
ans = Math.min(ans, r - l + 1);
// If the element at left
// is making it extra
if (++mapu[arr[l]] > (n / m))
c++;
// Update the left pointer
l++;
}
}
// Update the right pointer
r++;
}
// Return the resultant length
return ans;
}
// Driver Code
let arr = [ 1, 1, 2, 1, 1, 2 ];
let M = 2;
let N = arr.length;
document.write(minimumSubarray(arr, N, M));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Minimum removals required to make frequency of each array element equal to its value Given an array arr[] of size N, the task is to find the minimum count of array elements required to be removed such that frequency of each array element is equal to its value Examples: Input: arr[] = { 2, 4, 1, 4, 2 } Output: 2 Explanation: Removing arr[1] from the array modifies arr[] to { 2, 1, 4,
11 min read
Minimum replacements required to make sum of all K-length subarrays equal Given an array arr[] consisting of N positive integers and an integer K, the task is to make the sum of all K-length subarrays equal by replacing minimum number of array elements with any integer. Examples: Input: arr[] = {3, 4, 3, 5, 6}, K = 2Output: 2Explanation: Operation 1: Replacing arr[3] by 4
7 min read
Minimum removals required to make frequency of all remaining array elements equal Given an array arr[] of size N, the task is to find the minimum number of array elements required to be removed such that the frequency of the remaining array elements become equal. Examples : Input: arr[] = {2, 4, 3, 2, 5, 3}Output: 2Explanation: Following two possibilities exists:1) Either remove
13 min read
Find the minimum number of operations required to make all array elements equal Given an array arr[] of size N. The task is to make all the array elements equal by applying the below operations minimum number of times: Choose a pair of indices (i, j) such that |i - j| = 1 (indices i and j are adjacent) and set arr[i] = arr[i] + |arr[i] - arr[j]|Choose a pair of indices (i, j) s
6 min read
Maximize the minimum array element by M subarray increments of size S Given an array arr[] of N integers and two integers S and M, the task is to maximize the minimum array element by incrementing any subarray of size S by 1, M number of times. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, S = 2, M = 3Output: 3Explanation:Below are the operations performed:Operation 1:
10 min read
Count of replacements required to make the sum of all Pairs of given type from the Array equal Given an integer array arr of length N and an integer K, the task is to find the number of array elements to be replaced by a value from the range [1, K] such that each pair (arr[i], arr[N - 1 - i] have equal sum. Examples : Input: arr[] = {1, 2, 2, 1}, K = 3 Output: 1 Explanation: Replace arr[0] wi
12 min read