Given an array arr[] containing n numbers. The problem is to check whether the product of the given n numbers is even or odd.
Examples:
Input: arr[] = {2, 4, 3, 5}
Output: Even
Explanation: Product = 2 * 4 * 3 * 5 = 120, 120 is even.Input: arr[] = {3, 9, 7, 1}
Output: Odd
A simple solution is to first find the product, then check if the product is even or odd. This solution causes overflow for large arrays.
A better solution is based on following mathematical calculation facts:
- Product of two even numbers is even.
- Product of two odd numbers is odd.
- Product of one even and one odd number is even.
Based on the above facts, if a single even number occurs then the entire product of n numbers will be even else odd.
// 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 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 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# 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 |
<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> |
<?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 ?> |
Even
Time Complexity: O(n).
Auxiliary Space: O(1)
Approach 2: Using Memoization:
We can use memoization to optimize the function if the same function call with the same arguments is expected to occur multiple times. Here is an example implementation of the function that uses memoization:
#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 program to check whether product of // 'n' numbers is even or odd import java.util.*;
public class Main {
// memoization table to store previously
// computed results
static HashMap<String, Boolean> memo = new HashMap<>();
// function to check whether product of
// 'n' numbers is even or odd
static boolean isProductEven( int arr[], int n) {
// convert array to string for memoization
String key = Arrays.toString(arr);
// check if result is already computed
if (memo.containsKey(key)) {
return memo.get(key);
}
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 ) {
memo.put(key, true );
return true ;
}
}
// product is an odd number
memo.put(key, false );
return false ;
}
// Driver program to test above
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" );
}
}
} |
# memoization table to store previously computed results memo = {}
# function to check whether product of 'n' numbers is even or odd def isProductEven(arr, n):
# convert array to string for memoization
key = str (arr)
# check if result is already computed
if key in memo:
return memo[key]
for i in range (n):
# if a single even number is found, then
# final product will be an even number
if arr[i] % 2 = = 0 :
memo[key] = True
return True
# product is an odd number
memo[key] = False
return False
# Driver program to test above arr = [ 2 , 4 , 3 , 5 ]
n = len (arr)
if isProductEven(arr, n):
print ( "Even" )
else :
print ( "Odd" )
|
using System;
using System.Collections.Generic;
public class MainClass
{ // memoization table to store previously
// computed results
static Dictionary< string , bool > memo = new Dictionary< string , bool >();
// function to check whether product of
// 'n' numbers is even or odd
static bool IsProductEven( int [] arr, int n)
{
// convert array to string for memoization
string key = string .Join( "," , arr);
// check if result is already computed
if (memo.ContainsKey(key)) {
return memo[key];
}
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) {
memo[key] = true ;
return true ;
}
}
// product is an odd number
memo[key] = false ;
return false ;
}
// Driver program to test above
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" );
}
}
} |
// JS equivalent // memoization table to store previously computed results let memo = {}; // function to check whether product of 'n' numbers is even or odd function isProductEven(arr, n) {
// convert array to string for memoization
let key = arr.toString();
// check if result is already computed
if (key in memo) {
return memo[key];
}
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] % 2 === 0) {
memo[key] = true ;
return true ;
}
}
// product is an odd number
memo[key] = false ;
return false ;
} // Driver program to test above let arr = [2, 4, 3, 5]; let n = arr.length; if (isProductEven(arr, n)) {
console.log( "Even" );
} else {
console.log( "Odd" );
} |
Even
Time Complexity: O(N).
Auxiliary Space: O(1), a constant amount of extra space to store the intermediate result
Approach 3:
Here’s another approach to check whether the product of ‘n’ numbers is even or odd:
- Traverse the given array of ‘n’ numbers and count the number of even elements.
-
If the count of even elements is greater than or equal to 1, then the product of ‘n’ numbers will be even. Otherwise, it will be odd.
Here’s the implementation of this approach in C++:
import java.util.*; public class SpecialPrime {
// Function to check if a number is prime
public static boolean isPrime( int x) {
if (x < 2) {
return false ;
}
for ( int i = 2; i <= Math. sqrt (x); i++) {
if (x % i == 0) {
return false ;
}
}
return true ;
}
// Function to check if a number is a special prime
public static boolean isSpecialPrime( int x) {
int s = 0;
for ( int i = 2; i <= x; i++) {
if (isPrime(i)) {
s += i;
}
}
return isPrime(s);
}
// Function to count special primes up to 'n' and determine if there are at least 'k' of them
public static String countSpecialPrimes( int n, int k) {
int count = 0;
for ( int i = 2; i <= n; i++) {
if (isSpecialPrime(i)) {
count++;
}
if (count >= k) {
return "YES" ;
}
}
return "NO" ;
}
public static void main(String[] args) {
int n = 27;
int k = 2;
// Call the function to count special primes and output the result
System.out.println(countSpecialPrimes(n, k));
}
} |
import java.util.Arrays;
public class GFG {
// function to check whether the product of 'n' numbers
// is even or odd
private static boolean isProductEven( int [] arr, int n) {
int evenCount = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % 2 == 0 ) {
evenCount++;
}
}
if (evenCount >= 1 ) {
return true ;
} else {
return false ;
}
}
// Driver program to test above
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" );
}
} |
# Function to check whether the product of 'n' numbers is even or odd def isProductEven(arr):
even_count = 0
for i in range ( len (arr)):
if arr[i] % 2 = = 0 :
even_count + = 1
if even_count > = 1 :
return True
else :
return False
# Driver program to test above function if __name__ = = "__main__" :
arr = [ 2 , 4 , 3 , 5 ]
if isProductEven(arr):
print ( "Even" )
else :
print ( "Odd" )
|
using System;
public class GFG {
// Function to check whether the product of 'n' numbers
// is even or odd
static bool IsProductEven( int [] arr, int n)
{
int evenCount = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % 2 == 0) {
evenCount++;
}
}
if (evenCount >= 1) {
return true ;
}
else {
return false ;
}
}
// Driver program to test above
public static void Main( string [] args)
{
int [] arr = { 2, 4, 3, 5 };
int n = arr.Length;
if (IsProductEven(arr, n))
Console.WriteLine( "Even" );
else
Console.WriteLine( "Odd" );
}
} |
//javascript code for the above approach. function isPrime(x) {
if (x < 2) {
return false ;
}
for (let i = 2; i <= Math.sqrt(x); i++) {
if (x % i === 0) {
return false ;
}
}
return true ;
} function isSpecialPrime(x) {
let s = 0;
for (let i = 2; i <= x; i++) {
if (isPrime(i)) {
s += i;
}
}
return isPrime(s);
} function countSpecialPrimes(n, k) {
let count = 0;
for (let i = 2; i <= n; i++) {
if (isSpecialPrime(i)) {
count++;
}
if (count >= k) {
return "YES" ;
}
}
return "NO" ;
} const n = 27; const k = 2; // Call the function to count special primes and output the result console.log(countSpecialPrimes(n, k)); |
Even
Time Complexity: O(n).
Auxiliary Space: O(1)