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