Number of Subsequences with Even and Odd Sum | Set 2 | Function to find number of Subsequences with Even and Odd Sum ; Counting number of odds ; Even count ; Total Subsequences is ( 2 ^ n - 1 ) For NumberOfEvenSubsequences subtract NumberOfOddSubsequences from total ; Driver code ; Calling the function
function countSum ( arr , n ) { let NumberOfOdds = 0 , NumberOfEvens = 0 ; for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) NumberOfOdds ++ ; NumberOfEvens = n - NumberOfOdds ; let NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; let NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; return [ NumberOfEvenSubsequences , NumberOfOddSubsequences ] ; } let arr = [ 1 , 2 , 2 , 3 ] ; let n = arr . length ; let ans = countSum ( arr , n ) ; document . write ( " " + ans [ 0 ] ) ; document . write ( " " + ans [ 1 ] ) ;