Count of Subsets of a given Set with element X present in it | C ++ implementation of above approach ; Function to calculate ( 2 ^ ( n - 1 ) ) ; Initially initialize answer to 1 ; If e is odd , multiply b with answer ; Function to count subsets in which X element is present ; Check if X is present in given subset or not ; If X is present in set then calculate 2 ^ ( n - 1 ) as count ; if X is not present in a given set ; Driver Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculatePower ( int b , int e ) { int ans = 1 ; while ( e > 0 ) { if ( e % 2 == 1 ) ans = ans * b ; e = e / 2 ; b = b * b ; } return ans ; } int CountSubSet ( int arr [ ] , int n , int X ) { int count = 0 , checkX = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == X ) { checkX = 1 ; break ; } } if ( checkX == 1 ) count = calculatePower ( 2 , n - 1 ) ; else count = 0 ; return count ; } int main ( ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountSubSet ( arr , n , X ) ; return 0 ; }