📜  所有元素的频率相同的最大子数组

📅  最后修改于: 2021-05-14 01:33:46             🧑  作者: Mango

给定N个整数的数组arr [] ,任务是查找所有子元素的频率相同的最大子数组的大小。

例子:

方法:想法是生成所有可能的子数组,并检查每个子数组是否有任何子数组具有所有元素的频率。步骤如下:

  1. 生成所有可能的子数组。
  2. 对于每个子数组,采用两个映射,一个映射存储每个元素的频率,第二个映射存储给定频率的元素数量。
  3. 如果对于任何子数组,第二个图的大小等于1 ,则意味着每个元素在子数组中具有相同的频率。
  4. 返回所有此类子数组的最大大小。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
#include 
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 map1;
 
        // Map 2 to hash frequency
        // of all frequencies of
        // elements
        unordered_map 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;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
  // Function to find maximum subarray size
  static 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
      HashMap map1 = new HashMap<>();
 
      // Map 2 to hash frequency
      // of all frequencies of
      // elements
      HashMap map2 = new HashMap<>();
 
      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.containsKey(arr[j]))
        {
          ele_count = 0;
        }
        else
        {
          ele_count = map1.get(arr[j]);
        }
 
        // Increasing frequency of arr[j]
        // by 1
        if (map1.containsKey(arr[j]))
        {
          map1.put(arr[j],map1.get(arr[j])+1);
        }
        else
        {
          map1.put(arr[j], 1);
        }
 
        // Check if previous frequency
        // is present in map 2
        if (map2.containsKey(ele_count))
        {
 
          // Delete previous frequency
          // if hash is equal to 1
          if (map2.get(ele_count) == 1)
          {
            map2.remove(ele_count);
          }
          else
          {
 
            // Decrement the hash of
            // previous frequency
            map2.put(ele_count,map2.get(ele_count) - 1);
          }
        }
 
        // Incrementing hash of new
        // frequency in map 2
        if (map2.containsKey(ele_count + 1))
        {
          map2.put(ele_count + 1, map2.get(ele_count + 1) + 1);
        }
        else
        {
          map2.put(ele_count + 1, 1);
        }
 
        // Check if map2 size is 1
        // and updating answer
        if (map2.size() == 1)
          ans = Math.max(ans, j - i + 1);
      }
    }
 
    // Return the maximum size of subarray
    return ans;
  }
 
  // Driver Code
  public static void main(String []args)
  {
 
    // Given array arr[]
    int[] arr = { 1, 2, 2, 5, 6, 5, 6 };
    int N = arr.length;
 
    // Function Call
    System.out.println(max_subarray_size(N, arr));
  }
}
 
// This code is contributed by rutvik_56.


Python3
# Python3 program for the above approach
 
# Function to find maximum subarray size
def max_subarray_size(N, arr):
 
    ans = 0
 
    # Generating all subarray
    # i -> starting index
    # j -> end index
    for i in range(N):
         
        # Map 1 to hash frequency
        # of all elements in subarray
        map1 = {}
 
        # Map 2 to hash frequency
        # of all frequencies of
        # elements
        map2 = {}
 
        for j in range(i, N):
         
            # ele_count is the previous
            # frequency of arr[j]
 
            # Finding previous frequency of
            # arr[j] in map 1
            if (arr[j] not in map1):
                ele_count = 0
            else:
                ele_count = map1[arr[j]]
 
            # Increasing frequency of arr[j]
            # by 1
            if arr[j] in map1:
                map1[arr[j]] += 1
            else:
                map1[arr[j]] = 1
 
            # Check if previous frequency
            # is present in map 2
            if (ele_count in map2):
         
                # Delete previous frequency
                # if hash is equal to 1
                if (map2[ele_count] == 1):
                    del map2[ele_count]
                else:
                 
                    # Decrement the hash of
                    # previous frequency
                    map2[ele_count] -= 1
 
            # Incrementing hash of new
            # frequency in map 2
            if ele_count + 1 in map2:
                map2[ele_count + 1] += 1
            else:
                map2[ele_count + 1] = 1
 
            # Check if map2 size is 1
            # and updating answer
            if (len(map2) == 1):
                ans = max(ans, j - i + 1)
 
    # Return the maximum size of subarray
    return ans
 
# Driver Code
 
# Given array arr[]
arr = [ 1, 2, 2, 5, 6, 5, 6 ]
 
N = len(arr)
 
# Function Call
print(max_subarray_size(N, arr))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
// Function to find maximum subarray size
static 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
        Dictionary map1 = new Dictionary();
  
        // Map 2 to hash frequency
        // of all frequencies of
        // elements
        Dictionary map2 = new Dictionary();
  
        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.ContainsKey(arr[j]))
            {
                ele_count = 0;
            }
            else
            {
                ele_count = map1[arr[j]];
            }
  
            // Increasing frequency of arr[j]
            // by 1
            if (map1.ContainsKey(arr[j]))
            {
                map1[arr[j]]++;
            }
            else
            {
                map1.Add(arr[j], 1);
            }
  
            // Check if previous frequency
            // is present in map 2
            if (map2.ContainsKey(ele_count))
            {
                 
                // Delete previous frequency
                // if hash is equal to 1
                if (map2[ele_count] == 1)
                {
                    map2.Remove(ele_count);
                }
                else
                {
                     
                    // Decrement the hash of
                    // previous frequency
                    map2[ele_count]--;
                }
            }
  
            // Incrementing hash of new
            // frequency in map 2
            if (map2.ContainsKey(ele_count + 1))
            {
                map2[ele_count + 1]++;
            }
            else
            {
                map2.Add(ele_count + 1, 1);
            }
  
            // Check if map2 size is 1
            // and updating answer
            if (map2.Count == 1)
                ans = Math.Max(ans, j - i + 1);
        }
    }
  
    // Return the maximum size of subarray
    return ans;
}
 
// Driver Code
static void Main()
{
     
    // Given array arr[]
    int[] arr = { 1, 2, 2, 5, 6, 5, 6 };
  
    int N = arr.Length;
  
    // Function Call
    Console.WriteLine(max_subarray_size(N, arr));
}
}
 
// This code is contributed by divyesh072019


输出:

6

时间复杂度: O(N 2 )
辅助空间: O(N)