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++
#include <bits/stdc++.h>
using namespace std;
bool isProductEven( int arr[], int n)
{
for ( int i = 0; i < n; i++)
if ((arr[i] & 1) == 0)
return true ;
return false ;
}
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
public class GFG {
static boolean isProductEven( int arr[], int n)
{
for ( int i = 0 ; i < n; i++)
if ((arr[i] & 1 ) == 0 )
return true ;
return false ;
}
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" ) ;
}
}
|
Python3
def isProductEven(arr, n):
for i in range ( 0 , n):
if ((arr[i] & 1 ) = = 0 ):
return True
return False
arr = [ 2 , 4 , 3 , 5 ]
n = len (arr)
if (isProductEven(arr, n)):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
class GFG
{
static bool isProductEven( int []arr, int n)
{
for ( int i = 0; i < n; i++)
if ((arr[i] & 1) == 0)
return true ;
return false ;
}
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" ) ;
}
}
|
Javascript
<script>
function isProductEven(arr, n)
{
for (let i = 0; i < n; i++)
if ((arr[i] & 1) == 0)
return true ;
return false ;
}
let arr = [ 2, 4, 3, 5 ];
let n = arr.length;
if (isProductEven(arr, n))
document.write( "Even" );
else
document.write( "Odd" );
</script>
|
PHP
<?php
function isProductEven( $arr , $n )
{
for ( $i = 0; $i < $n ; $i ++)
if (( $arr [ $i ] & 1) == 0)
return true;
return false;
}
$arr = array ( 2, 4, 3, 5 );
$n = sizeof( $arr );
if (isProductEven( $arr , $n ))
echo "Even" ;
else
echo "Odd" ;
?>
|
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:
C++
#include <bits/stdc++.h>
using namespace std;
bool isProductEven( int arr[], int n)
{
for ( int i = 0; i < n; i++)
{
if ((arr[i] & 1) == 0)
return true ;
}
return false ;
}
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
import java.util.*;
public class Main {
static HashMap<String, Boolean> memo = new HashMap<>();
static boolean isProductEven( int arr[], int n) {
String key = Arrays.toString(arr);
if (memo.containsKey(key)) {
return memo.get(key);
}
for ( int i = 0 ; i < n; i++) {
if ((arr[i] & 1 ) == 0 ) {
memo.put(key, true );
return true ;
}
}
memo.put(key, false );
return false ;
}
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" );
}
}
}
|
Python3
memo = {}
def isProductEven(arr, n):
key = str (arr)
if key in memo:
return memo[key]
for i in range (n):
if arr[i] % 2 = = 0 :
memo[key] = True
return True
memo[key] = False
return False
arr = [ 2 , 4 , 3 , 5 ]
n = len (arr)
if isProductEven(arr, n):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
using System.Collections.Generic;
public class MainClass
{
static Dictionary< string , bool > memo = new Dictionary< string , bool >();
static bool IsProductEven( int [] arr, int n)
{
string key = string .Join( "," , arr);
if (memo.ContainsKey(key)) {
return memo[key];
}
for ( int i = 0; i < n; i++)
{
if ((arr[i] & 1) == 0) {
memo[key] = true ;
return true ;
}
}
memo[key] = false ;
return false ;
}
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" );
}
}
}
|
Javascript
let memo = {};
function isProductEven(arr, n) {
let key = arr.toString();
if (key in memo) {
return memo[key];
}
for (let i = 0; i < n; i++) {
if (arr[i] % 2 === 0) {
memo[key] = true ;
return true ;
}
}
memo[key] = false ;
return false ;
}
let arr = [2, 4, 3, 5];
let n = arr.length;
if (isProductEven(arr, n)) {
console.log( "Even" );
} else {
console.log( "Odd" );
}
|
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++:
C++
import java.util.*;
public class SpecialPrime {
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 ;
}
public static boolean isSpecialPrime( int x) {
int s = 0;
for ( int i = 2; i <= x; i++) {
if (isPrime(i)) {
s += i;
}
}
return isPrime(s);
}
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;
System.out.println(countSpecialPrimes(n, k));
}
}
|
Java
import java.util.Arrays;
public class GFG {
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 ;
}
}
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" );
}
}
|
Python3
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
if __name__ = = "__main__" :
arr = [ 2 , 4 , 3 , 5 ]
if isProductEven(arr):
print ( "Even" )
else :
print ( "Odd" )
|
C#
using System;
public class GFG {
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 ;
}
}
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
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;
console.log(countSpecialPrimes(n, k));
|
Time Complexity: O(n).
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!