Length of longest subarray having frequency of every element equal to K
Last Updated :
11 Jul, 2022
Given an array arr[] consisting of N integers and an integer K, the task is to find the length of the longest subarray such that each element occurs K times.
Examples:
Input: arr[] = {3, 5, 2, 2, 4, 6, 4, 6, 5}, K = 2
Output: 8
Explanation: The subarray: {5, 2, 2, 4, 6, 4, 6, 5} of length 8 has frequency of every element as 2.
Input: arr[] = {5, 5, 5, 5}, K = 3
Output: 3
Explanation: The subarray: {5, 5, 5} of length 3 has frequency of every element as 3.
Approach: Follow the steps below to solve the problem:
- Generate all possible subarrays from the given array.
- For each subarray, initialize two unordered maps map1 and map2, to store the frequency of each element and store the count of elements with the respective frequencies.
- If for any subarray, the size of map2 is equal to 1 and the frequency of the current element is K, that means every element individually appears K times in the current subarray.
- Finally, return the maximum size of all such subarrays.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max_subarray_len( int arr[],
int N, int K)
{
int ans = 0;
for ( int i = 0; i < N; i++) {
unordered_map< int , int > map1;
unordered_map< int , int > map2;
for ( int j = i; j < N; j++) {
int prev_freq;
if (map1.find(arr[j])
== map1.end()) {
prev_freq = 0;
}
else {
prev_freq = map1[arr[j]];
}
map1[arr[j]]++;
if (map2.find(prev_freq)
!= map2.end()) {
if (map2[prev_freq] == 1) {
map2.erase(prev_freq);
}
else {
map2[prev_freq]--;
}
}
int new_freq = prev_freq + 1;
map2[new_freq]++;
if (map2.size() == 1
&& (new_freq) == K) {
ans = max(
ans, j - i + 1);
}
}
}
return ans;
}
int main()
{
int arr[] = { 3, 5, 2, 2, 4,
6, 4, 6, 5 };
int K = 2;
int N = sizeof (arr)
/ sizeof (arr[0]);
cout << max_subarray_len(
arr, N, K);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int max_subarray_len( int arr[],
int N, int K)
{
int ans = 0 ;
for ( int i = 0 ; i < N; i++)
{
HashMap<Integer,Integer> map1 = new HashMap<>();
HashMap<Integer,Integer> map2 = new HashMap<>();
for ( int j = i; j < N; j++)
{
int prev_freq = 0 ;
if (!map1.containsKey(arr[j]))
{
prev_freq = 0 ;
}
else
{
prev_freq = map1.get(arr[j]);
}
if (map1.containsKey(arr[j]))
{
map1.put(arr[j], map1.get(arr[j]) + 1 );
}
else
{
map1.put(arr[j], 1 );
}
if (map2.containsKey(prev_freq))
{
if (map2.get(prev_freq) == 1 )
{
map2.remove(prev_freq);
}
else
{
map2.put(prev_freq, map2.get(prev_freq)- 1 );
}
}
int new_freq = prev_freq + 1 ;
if (map2.containsKey(new_freq))
{
map2.put(new_freq, map2.get(new_freq) + 1 );
}
else {
map2.put(new_freq, 1 );
}
if (map2.size() == 1
&& (new_freq) == K) {
ans = Math.max(
ans, j - i + 1 );
}
}
}
return ans;
}
public static void main(String[] args)
{
int arr[] = { 3 , 5 , 2 , 2 , 4 ,
6 , 4 , 6 , 5 };
int K = 2 ;
int N = arr.length;
System.out.print(max_subarray_len(
arr, N, K));
}
}
|
Python3
from collections import defaultdict
def max_subarray_len(arr, N, K):
ans = 0
for i in range (N):
map1 = defaultdict( int )
map2 = defaultdict( int )
for j in range (i, N):
if (arr[j] not in map1):
prev_freq = 0
else :
prev_freq = map1[arr[j]]
map1[arr[j]] + = 1
if prev_freq in map2:
if (map2[prev_freq] = = 1 ):
del map2[prev_freq]
else :
map2[prev_freq] - = 1
new_freq = prev_freq + 1
map2[new_freq] + = 1
if ( len (map2) = = 1 and
(new_freq) = = K):
ans = max (ans, j - i + 1 )
return ans
if __name__ = = "__main__" :
arr = [ 3 , 5 , 2 , 2 , 4 ,
6 , 4 , 6 , 5 ]
K = 2
N = len (arr)
print (max_subarray_len(
arr, N, K))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int max_subarray_len( int []arr,
int N, int K)
{
int ans = 0;
for ( int i = 0; i < N; i++)
{
Dictionary< int , int > map1 = new Dictionary< int , int >();
Dictionary< int , int > map2 = new Dictionary< int , int >();
for ( int j = i; j < N; j++)
{
int prev_freq = 0;
if (!map1.ContainsKey(arr[j]))
{
prev_freq = 0;
}
else
{
prev_freq = map1[arr[j]];
}
if (map1.ContainsKey(arr[j]))
{
map1[arr[j]] = map1[arr[j]] + 1;
}
else
{
map1.Add(arr[j], 1);
}
if (map2.ContainsKey(prev_freq))
{
if (map2[prev_freq] == 1)
{
map2.Remove(prev_freq);
}
else
{
map2[prev_freq]= map2[prev_freq]-1;
}
}
int new_freq = prev_freq + 1;
if (map2.ContainsKey(new_freq))
{
map2[new_freq] = map2[new_freq] + 1;
}
else {
map2.Add(new_freq, 1);
}
if (map2.Count == 1
&& (new_freq) == K) {
ans = Math.Max(
ans, j - i + 1);
}
}
}
return ans;
}
public static void Main(String[] args)
{
int []arr = { 3, 5, 2, 2, 4,
6, 4, 6, 5 };
int K = 2;
int N = arr.Length;
Console.Write(max_subarray_len(
arr, N, K));
}
}
|
Javascript
<script>
function max_subarray_len(arr,N,K)
{
let ans = 0;
for (let i = 0; i < N; i++)
{
let map1 = new Map();
let map2 = new Map();
for (let j = i; j < N; j++)
{
let prev_freq = 0;
if (!map1.has(arr[j]))
{
prev_freq = 0;
}
else
{
prev_freq = map1.get(arr[j]);
}
if (map1.has(arr[j]))
{
map1.set(arr[j], map1.get(arr[j]) + 1);
}
else
{
map1.set(arr[j], 1);
}
if (map2.has(prev_freq))
{
if (map2.get(prev_freq) == 1)
{
map2. delete (prev_freq);
}
else
{
map2.set(prev_freq, map2.get(prev_freq)-1);
}
}
let new_freq = prev_freq + 1;
if (map2.has(new_freq))
{
map2.set(new_freq, map2.get(new_freq) + 1);
}
else
{
map2.set(new_freq, 1);
}
if (map2.size == 1
&& (new_freq) == K)
{
ans = Math.max(
ans, j - i + 1);
}
}
}
return ans;
}
let arr=[ 3, 5, 2, 2, 4,
6, 4, 6, 5 ];
let K = 2;
let N = arr.length;
document.write(max_subarray_len(
arr, N, K));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N)
Related Topic: Subarrays, Subsequences, and Subsets in Array
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...