Comprobar si el producto de ‘n’ números es par o impar

Dada una array arr[] que contiene n números. El problema es comprobar si el producto de los n números dados es par o impar.
Ejemplos: 
 

Input : arr[] = {2, 4, 3, 5}
Output : Even
Product = 2 * 4 * 3 * 5 = 120

Input : arr[] = {3, 9, 7, 1}
Output : Odd

Una solución simple es primero encontrar el producto y luego verificar si el producto es par o impar. Esta solución provoca el desbordamiento de arrays grandes.
Una mejor solución se basa en los siguientes hechos de cálculo matemático:
 

  1. El producto de dos números pares es par.
  2. El producto de dos números impares es impar.
  3. El producto de un número par y uno impar es par.

Con base en los hechos anteriores, si ocurre un solo número par, entonces el producto completo de n números será par o impar.
 

C++

// C++ implementation to check whether product of
// 'n' numbers is even or odd
#include <bits/stdc++.h>
 
using namespace std;
 
// function to check whether product of
// 'n' numbers is even or odd
bool isProductEven(int arr[], int n)
{
    for (int i = 0; i < n; i++)
 
        // if a single even number is found, then
        // final product will be an even number
        if ((arr[i] & 1) == 0)
            return true;
 
    // product is an odd number
    return false;
}
 
// Driver program to test above
int main()
{
    int arr[] = { 2, 4, 3, 5 };
    int n = sizeof(arr) / sizeof(arr[0]);
    if (isProductEven(arr, n))
        cout << "Even";
    else
        cout << "Odd";
    return 0;
}

Java

// Java implementation to check whether product of
// 'n' numbers is even or odd
 
public class GFG {
     
    // function to check whether product of
    // 'n' numbers is even or odd
    static boolean isProductEven(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
 
            // if a single even number is found, then
            // final product will be an even number
            if ((arr[i] & 1) == 0)
                return true;
 
        // product is an odd number
        return false;
    }
 
 
    // Driver code
    public static void main(String args[])
    {
            int arr[] = { 2, 4, 3, 5 };
            int n = arr.length ;
            if (isProductEven(arr, n))
                System.out.println("Even");
            else
                System.out.println("Odd") ;     
    }
    // This Code is contributed by ANKITRAI1
}

Python3

# Python3 implementation to
# check whether product of 'n'
# numbers is even or odd
 
# function to check whether
# product of 'n' numbers is
# even or odd
def isProductEven(arr, n):
 
    for i in range(0, n):
 
        # if a single even number is
        # found, then final product
        # will be an even number
        if ((arr[i] & 1) == 0):
            return True
 
    # product is an odd number
    return False
 
# Driver Code
arr = [ 2, 4, 3, 5 ]
n = len(arr)
if (isProductEven(arr, n)):
    print("Even")
else:
    print("Odd")
 
# This code is contributed
# by ihritik

C#

// C# implementation to check
// whether product of 'n'
// numbers is even or odd
using System;
 
class GFG
{
     
// function to check whether
// product of 'n' numbers
// is even or odd
static bool isProductEven(int []arr, int n)
{
    for (int i = 0; i < n; i++)
 
        // if a single even number is
        // found, then final product
        // will be an even number
        if ((arr[i] & 1) == 0)
            return true;
 
    // product is an odd number
    return false;
}
 
// Driver code
public static void Main()
{
    int []arr = { 2, 4, 3, 5 };
    int n = arr.Length;
    if (isProductEven(arr, n))
        Console.WriteLine("Even");
    else
        Console.WriteLine("Odd") ;
}
}
 
// This code is contributed by ihritik

PHP

<?php
// PHP implementation to check
// whether product of 'n' numbers
// is even or odd
 
// function to check whether
// product of 'n' numbers is
// even or odd
function isProductEven($arr, $n)
{
    for ($i = 0; $i < $n; $i++)
 
        // if a single even number is
        // found, then final product
        // will be an even number
        if (($arr[$i] & 1) == 0)
            return true;
 
    // product is an odd number
    return false;
}
 
// Driver code
$arr = array( 2, 4, 3, 5 );
$n = sizeof($arr);
if (isProductEven($arr, $n))
    echo "Even";
else
    echo "Odd";
     
// This code is contributed by ihritik
?>

Javascript

<script>
// JavaScript implementation to check whether product of
// 'n' numbers is even or odd
 
// function to check whether product of
// 'n' numbers is even or odd
function isProductEven(arr, n)
{
    for (let i = 0; i < n; i++)
 
        // if a single even number is found, then
        // final product will be an even number
        if ((arr[i] & 1) == 0)
            return true;
 
    // product is an odd number
    return false;
}
 
// Driver program to test above
  
    let arr = [ 2, 4, 3, 5 ];
    let n = arr.length;
    if (isProductEven(arr, n))
        document.write("Even");
    else
        document.write("Odd");
 
// This code is contributed by Surbhi Tyagi.
 
</script>

Producción: 

Even

Complejidad temporal: O(n).
 

Publicación traducida automáticamente

Artículo escrito por ayushjauhari14 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *