text: Number of Subsequences with Even and Odd Sum | JavaScript implementation to find the number of Subsequences with Even and Odd Sum ; Arrays to store the count of even subsequences and odd subsequences ; Initialising countEVEN [ 0 ] and countODD [ 0 ] to 0 since as there is no subsequence before the iteration with even or odd count . ; Here countODD [ i ] denotes count of odd subsequences till i ; if the number is even ; if the number is odd ; Driver Code
code: function countSum ( arr , n ) { var countODD = Array ( n + 1 ) . fill ( 0 ) ; var countEVEN = Array ( n + 1 ) . fill ( 0 ) ; countODD [ 0 ] = 0 ; countEVEN [ 0 ] = 0 ; for ( var i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 == 0 ) { countEVEN [ i ] = countEVEN [ i - 1 ] + countEVEN [ i - 1 ] + 1 ; countODD [ i ] = countODD [ i - 1 ] + countODD [ i - 1 ] ; } else { countEVEN [ i ] = countEVEN [ i - 1 ] + countODD [ i - 1 ] ; countODD [ i ] = countODD [ i - 1 ] + countEVEN [ i - 1 ] + 1 ; } } var ans = [ 0 , 0 ] ; ans [ 0 ] = countEVEN [ n ] ; ans [ 1 ] = countODD [ n ] ; return ans ; } var arr = [ 1 , 2 , 2 , 3 ] ; var n = 4 ; var ans = countSum ( arr , n ) ; document . write ( " " + ans [ 0 ] ) ; document . write ( " " + ans [ 1 ] ) ;