Number of Subsequences with Even and Odd Sum | Set 2 | CPP program to find number of Subsequences with Even and Odd Sum ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > countSum ( int arr [ ] , int n ) { int NumberOfOdds = 0 , NumberOfEvens = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) NumberOfOdds ++ ; NumberOfEvens = n - NumberOfOdds ; int NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; int NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; return { NumberOfEvenSubsequences , NumberOfOddSubsequences } ; } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pair < int , int > ans = countSum ( arr , n ) ; cout << " EvenSum ▁ = ▁ " << ans . first ; cout << " ▁ OddSum ▁ = ▁ " << ans . second ; return 0 ; }