Dada una array arr[] de elementos enteros, la tarea es encontrar el número total de subconjuntos de arr[] en los que el producto de los elementos es par.
Ejemplos:
Entrada: arr[] = {2, 2, 3}
Salida: 6
Todos los subconjuntos posibles son {2}, {2}, {2, 2}, {2, 3}, {2, 3} y {2 , 2, 3}Entrada: arr[] = {3, 3, 3}
Salida: 6
Enfoque: Ya sabemos que:
- par * par = par
- Impar * Par = Par
- Impar * Impar = Impar
Ahora, necesitamos contar los subconjuntos totales en los que está presente al menos un elemento par para que el producto de los elementos sea par.
Ahora, Número total de subconjuntos que tienen al menos un elemento par = Total de subconjuntos posibles de n – Total de subconjuntos que tienen todos los elementos impares,
es decir , (2 n – 1) – (2 totalOdd – 1)
A continuación se muestra la implementación de el enfoque anterior:
C++
// 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; }
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
<?php // PHP implementation of above approach // Function to find total number of subsets // in which product of the elements is even function find(&$a, $n) { $count_odd = 0; for($i = 0; $i < $n ; $i++) { // counting number of odds elements if ($i % 2 != 0) $count_odd += 1; } $result = pow(2, $n) - 1 ; $result -= (pow(2, $count_odd) - 1) ; echo $result ."\n"; } // Driver code $a = array(2, 2, 3) ; $n = sizeof($a)/sizeof($a[0]) ; // function calling find($a,$n); return 0; ?>
Javascript
<script> // Javascript implementation of above approach // Function to find total number of subsets // in which product of the elements is even function find(a, n) { var count_odd = 0; for(var i = 0; i < n ; i++) { // counting number of odds elements if (i % 2 != 0) count_odd += 1; } var result = Math.pow(2, n) - 1 ; result -= (Math.pow(2, count_odd) - 1) ; document.write( result ); } // Driver code var a = [2, 2, 3]; var n = a.length; // function calling find(a,n); </script>
6