Maximum length of subarray such that sum of the subarray is even | Function to find Length of the longest subarray such that Sum of the subarray is even ; Check if Sum of complete array is even ; if ( Sum % 2 == 0 ) : total Sum is already even ; Find an index i such the a [ i ] is odd and compare Length of both halfs excluding a [ i ] to find max Length subarray ; Driver Code
def maxLength ( a , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE Len = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE return n NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT Len = max ( Len , max ( n - i - 1 , i ) ) NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT a = [ 1 , 2 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( maxLength ( a , n ) ) NEW_LINE