f the number of subsets of a set with 2006 elements having an even number of elements is 2k, then find the sum of digits of k.
Answers
Answer:
Input: arr[] = {2, 2, 3}
Output: 6
All possible sub-sets are {2}, {2}, {2, 2}, {2, 3}, {2, 3} and {2, 2, 3}
Input: arr[] = {3, 3, 3}
Output: 0
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: We already know that :
Even * Even = Even
Odd * Even = Even
Odd * Odd = Odd
Now, we need to count the total subsets in which at least a single even element is present in order for the product of the elements to be even.
Now, Total number of sub-sets having at least one even element = Total possible sub-sets of n – Total sub-sets having all odd elements
i.e. (2n – 1) – (2totalOdd – 1)
Below is the implementation of the above approach:
filter_none
edit
play_arrow
brightness_4
// C++ implementation of above approach
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// Function to find total number of subsets
// in which product of the elements is even
void find(int a[], int n)
{
int count_odd = 0;
for(int i = 0; i < n ; i++)
{
// counting number of odds elements
if (i % 2 != 0)
count_odd += 1;
}
int result = pow(2, n) - 1 ;
result -= (pow(2, count_odd) - 1) ;
cout << result << endl;
}
// Driver code
int main()
{
int a[] = {2, 2, 3} ;
int n = sizeof(a)/sizeof(a[0]) ;
// function calling
find(a,n);
return 0;
// This code is contributed by ANKITRAI1;
}
Explanation: