Total number of subsets in which the product of the elements is even (original) (raw)

Last Updated : 9 Sep, 2022

Given an array arr[] of integer elements, the task is to find the total number of sub-sets of arr[] in which the product of the elements is even.

Examples:

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: 6

Approach: We already know that:

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:

C++ `

// C++ implementation of above approach

#include #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; }

Java

// Java implementation of above approach

class GFG {

// Function to find total number of subsets // in which product of the elements is even static 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 = (int) (Math.pow(2, n) - 1);
    result -= (Math.pow(2, count_odd) - 1);
    System.out.println(result);

}

// Driver code public static void main(String[] args) { int a[] = {2, 2, 3}; int n = a.length;

// function calling find(a, n);

}

} //this code contributed by 29AJayKumar

Python3

Python3 implementation of above approach

import math as ma

Function to find total number of subsets

in which product of the elements is even

def find(a): count_odd = 0 for i in a:

    # counting number of odds elements
    if(i % 2 != 0):
        count_odd+= 1

result = pow(2, len(a)) - 1
result = result - (pow(2, count_odd) - 1)
print(result)

Driver code

a =[2, 2, 3] find(a)

C#

// C# implementation of above approach using System; public class GFG {

// Function to find total number of subsets // in which product of the elements is even static 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 = (int) (Math.Pow(2, n) - 1);
    result -= (int)(Math.Pow(2, count_odd) - 1);
    Console.Write(result);

}

// Driver code public static void Main() { int []a = {2, 2, 3}; int n = a.Length;

// function calling find(a, n);

}

} //this code contributed by 29AJayKumar

PHP

i<i < i<n ; $i++) { // counting number of odds elements if ($i % 2 != 0) $count_odd += 1; } result=pow(2,result = pow(2, result=pow(2,n) - 1 ; result−=(pow(2,result -= (pow(2, result=(pow(2,count_odd) - 1) ; echo $result ."\n"; } // Driver code a=array(2,2,3);<spanclass="katex"><spanclass="katex−mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mi>o</mi><mi>f</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/x−tex">n=sizeof(</annotation></semantics></math></span><spanclass="katex−html"aria−hidden="true"><spanclass="base"><spanclass="strut"style="height:0.4306em;"></span><spanclass="mordmathnormal">n</span><spanclass="mspace"style="margin−right:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="margin−right:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;vertical−align:−0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">zeo</span><spanclass="mordmathnormal"style="margin−right:0.10764em;">f</span><spanclass="mopen">(</span></span></span></span>a)/sizeof(a = array(2, 2, 3) ; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mi>o</mi><mi>f</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">n = sizeof(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.4306em;"></span><span class="mord mathnormal">n</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">i</span><span class="mord mathnormal">zeo</span><span class="mord mathnormal" style="margin-right:0.10764em;">f</span><span class="mopen">(</span></span></span></span>a)/sizeof(a=array(2,2,3);<spanclass="katex"><spanclass="katexmathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>n</mi><mo>=</mo><mi>s</mi><mi>i</mi><mi>z</mi><mi>e</mi><mi>o</mi><mi>f</mi><mostretchy="false">(</mo></mrow><annotationencoding="application/xtex">n=sizeof(</annotation></semantics></math></span><spanclass="katexhtml"ariahidden="true"><spanclass="base"><spanclass="strut"style="height:0.4306em;"></span><spanclass="mordmathnormal">n</span><spanclass="mspace"style="marginright:0.2778em;"></span><spanclass="mrel">=</span><spanclass="mspace"style="marginright:0.2778em;"></span></span><spanclass="base"><spanclass="strut"style="height:1em;verticalalign:0.25em;"></span><spanclass="mordmathnormal">s</span><spanclass="mordmathnormal">i</span><spanclass="mordmathnormal">zeo</span><spanclass="mordmathnormal"style="marginright:0.10764em;">f</span><spanclass="mopen">(</span></span></span></span>a)/sizeof(a[0]) ; // function calling find($a,$n); return 0; ?>

JavaScript

`