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
def countSum ( arr , n ) : NEW_LINE INDENT NumberOfOdds = 0 ; NumberOfEvens = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT NumberOfOdds += 1 ; NEW_LINE DEDENT DEDENT NumberOfEvens = n - NumberOfOdds ; NEW_LINE NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; NEW_LINE NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; NEW_LINE return ( NumberOfEvenSubsequences , NumberOfOddSubsequences ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE ans = countSum ( arr , n ) ; NEW_LINE print ( " EvenSum ▁ = " , ans [ 0 ] , end = " ▁ " ) ; NEW_LINE print ( " OddSum ▁ = " , ans [ 1 ] ) ; NEW_LINE DEDENT