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
function maxLength ( a , n ) { let sum = 0 , len = 0 ; for ( let i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return n ; for ( let i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) len = Math . max ( len , Math . max ( n - i - 1 , i ) ) ; } return len ; } let a = [ 1 , 2 , 3 , 2 ] ; let n = a . length ; document . write ( maxLength ( a , n ) + " " ) ;