Maximum length of subarray such that all elements are equal in the subarray | Function to find the longest subarray with same element ; Check if the elements are same then we can increment the length ; Reinitialize j ; Compare the maximum length e with j ; Return max length ; Given list arr [ ] ; Function call
def longest_subarray ( arr , d ) : NEW_LINE INDENT ( i , j , e ) = ( 0 , 1 , 0 ) NEW_LINE for i in range ( d - 1 ) : NEW_LINE INDENT if arr [ i ] == arr [ i + 1 ] : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j = 1 NEW_LINE DEDENT if e < j : NEW_LINE INDENT e = j NEW_LINE DEDENT DEDENT return e NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE print ( longest_subarray ( arr , N ) ) NEW_LINE