Find number of subarrays with even sum | Javascript program to count number of sub - arrays whose sum is even using brute force Time Complexity - O ( N ^ 2 ) Space Complexity - O ( 1 ) ; Find sum of all subarrays and increment result if sum is even ; Driver Code
function countEvenSum ( arr , n ) { let result = 0 ; for ( let i = 0 ; i <= n - 1 ; i ++ ) { let sum = 0 ; for ( let j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } let arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] ; let n = arr . length ; document . write ( " " + " " ) ; document . write ( countEvenSum ( arr , n ) ) ;