Largest subarray with frequency of all elements same

Given an array arr[] of N integers, the task is to find the size of largest subarray with frequency of all elements same.

Examples:

Input: arr[] = {1, 2, 2, 5, 6, 5, 6}
Output: 6
Explanation:
The subarrary = {2, 2, 5, 6, 5, 6} has frequency of every element is 2.

Input: arr[] = {1, 1, 1, 1, 1}
Output: 5
Explanation:
The subarrary = {1, 1, 1, 1, 1} has frequency of every element is 5.

Approach: The idea is to generate all possible subarrays and check for each subarray whether any subarray has frequency of all elements or not. Below are the steps:



  1. Generate all possible subarrays.
  2. For each subarray, take two maps, one map to stores the frequency of every element and second map stores number of elements with given frequency.
  3. If for any subarray, size of second map becomes equal to 1, that means every element have same frequency in the subarray.
  4. Return the maximum size of all such subarrays.

Below is the implementation of above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
  
#include <iostream>
#include <unordered_map>
using namespace std;
  
// Function to find maximum subarray size
int max_subarray_size(int N, int arr[])
{
    int ans = 0;
  
    // Generating all subarray
    // i -> starting index
    // j -> end index
    for (int i = 0; i < N; i++) {
  
        // Map 1 to hash frequency
        // of all elements in subarray
        unordered_map<int, int> map1;
  
        // Map 2 to hash frequency
        // of all frequencies of
        // elements
        unordered_map<int, int> map2;
  
        for (int j = i; j < N; j++) {
  
            // ele_count is the previous
            // frequency of arr[j]
            int ele_count;
  
            // Finding previous frequency of
            // arr[j] in map 1
            if (map1.find(arr[j])
                == map1.end()) {
                ele_count = 0;
            }
            else {
                ele_count = map1[arr[j]];
            }
  
            // Increasing frequency of arr[j]
            // by 1
            map1[arr[j]]++;
  
            // Check if previous frequency
            // is present in map 2
            if (map2.find(ele_count)
                != map2.end()) {
  
                // Delete previous frequency
                // if hash is equal to 1
                if (map2[ele_count] == 1) {
                    map2.erase(ele_count);
                }
                else {
  
                    // Decrement the hash of
                    // previous frequency
                    map2[ele_count]--;
                }
            }
  
            // Incrementing hash of new
            // frequency in map 2
            map2[ele_count + 1]++;
  
            // Check if map2 size is 1
            // and updating answer
            if (map2.size() == 1)
                ans = max(ans, j - i + 1);
        }
    }
  
    // Return the maximum size of subarray
    return ans;
}
  
// Driver Code
int main()
{
    // Given array arr[]
    int arr[] = { 1, 2, 2, 5, 6, 5, 6 };
  
    int N = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    cout << max_subarray_size(N, arr);
    return 0;
}

chevron_right


Output:

6

Time Complexity: O(N2)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.