Maximum frequencies in each M | Function to find the frequency of the most common element in each M length subarrays ; Stores frequency of array element ; Stores the maximum frequency ; Iterate for the first sub - array and store the maximum ; Print the maximum frequency for the first subarray ; Iterate over the range [ M , N ] ; Subtract the A [ i - M ] and add the A [ i ] in the map ; Find the maximum frequency ; Print the maximum frequency for the current subarray ; Driver Code
def maxFrequencySubarrayUtil ( A , N , M ) : NEW_LINE INDENT i = 0 NEW_LINE m = { } NEW_LINE val = 0 NEW_LINE while ( i < M ) : NEW_LINE INDENT if A [ i ] in m : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A [ i ] ] = 1 NEW_LINE DEDENT val = max ( val , m [ A [ i ] ] ) NEW_LINE i += 1 NEW_LINE DEDENT print ( val , end = " ▁ " ) NEW_LINE for i in range ( M , N , 1 ) : NEW_LINE INDENT if A [ i - M ] in m : NEW_LINE INDENT m [ A [ i - M ] ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A [ i - M ] ] = 0 NEW_LINE DEDENT if A [ i ] in m : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE DEDENT val = 0 NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT val = max ( val , value ) NEW_LINE DEDENT print ( val , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 1 , 2 , 2 , 3 , 5 ] NEW_LINE N = len ( A ) NEW_LINE M = 4 NEW_LINE maxFrequencySubarrayUtil ( A , N , M ) NEW_LINE DEDENT