📜  元素乘积为偶数的子集总数

📅  最后修改于: 2021-04-23 18:28:39             🧑  作者: Mango

给定整数元素的数组arr [] ,任务是查找其中元素乘积为偶数的arr []子集的总数。

例子:

方法:我们已经知道:

  • 偶数*偶数=偶数
  • 奇数*偶数=偶数
  • 奇数*奇数=奇数

现在,我们需要计算至少存在一个偶数元素的总子集,以便元素的乘积是偶数。
现在,具有至少一个偶数元素的子集总数= n的可能子集总数–具有所有奇数元素的子集总数
(2 n – 1)–(2 totalOdd – 1)

下面是上述方法的实现:

C++
// C++ implementation of above approach
  
#include 
#include
  
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


输出:
6