Open In App

Minimum length of the subarray required to be replaced to make frequency of array elements equal to N / M

Last Updated : 04 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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>


Output: 

1

 

Time Complexity: O(N)
Auxiliary Space: O(N)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads