Open In App

Check whether product of 'n' numbers is even or odd

Last Updated : 06 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of size n, the task is to check whether the product of the given n numbers is even or odd. Even product is even, return true, else false.

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
Explanation: Product = 3 * 9 * 7 * 1 = 189, 189 is odd.

[Naive Approach] Finding Product of Array - O(n) time and O(1) space

The idea is to multiply all elements of the given array and check if the product is divisible by 2. If it's divisible by 2, then the product is even; otherwise, it's odd.

The issue with this code is that it may cause Integer Overflow for large numbers.

C++
// C++ program to Check whether product
// of 'n' numbers is even or odd
#include <bits/stdc++.h>
using namespace std;

bool isEven(vector<int> &arr) {
    int product = 1;
    for (int i = 0; i < arr.size(); i++) {
        product *= arr[i];
    }
    return (product % 2 == 0);
}

int main() {
    vector<int> arr = {2, 4, 3, 5};
    if (isEven(arr)) {
        cout << "Even" << endl;
    }
    else {
        cout << "Odd" << endl;
    }
    return 0;
}
Java
// Java program to Check whether product
// of 'n' numbers is even or odd

class GfG {

    static boolean isEven(int[] arr) {
        int product = 1;
        for (int i = 0; i < arr.length; i++) {
            product *= arr[i];
        }
        return (product % 2 == 0);
    }

    public static void main(String[] args) {
        int[] arr = {2, 4, 3, 5};
        if (isEven(arr)) {
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
    }
}
Python
# Python program to Check whether product
# of 'n' numbers is even or odd

def isEven(arr):
    product = 1
    for i in range(len(arr)):
        product *= arr[i]
    return (product % 2 == 0)

if __name__ == "__main__":
    arr = [2, 4, 3, 5]
    if isEven(arr):
        print("Even")
    else:
        print("Odd")
C#
// C# program to Check whether product
// of 'n' numbers is even or odd

using System;

class GfG {

    static bool isEven(int[] arr) {
        int product = 1;
        for (int i = 0; i < arr.Length; i++) {
            product *= arr[i];
        }
        return (product % 2 == 0);
    }

    static void Main(string[] args) {
        int[] arr = {2, 4, 3, 5};
        if (isEven(arr)) {
            Console.WriteLine("Even");
        } else {
            Console.WriteLine("Odd");
        }
    }
}
JavaScript
// JavaScript program to Check whether product
// of 'n' numbers is even or odd

function isEven(arr) {
    let product = 1;
    for (let i = 0; i < arr.length; i++) {
        product *= arr[i];
    }
    return (product % 2 === 0);
}

let arr = [2, 4, 3, 5];
if (isEven(arr)) {
    console.log("Even");
} else {
    console.log("Odd");
}

Output
Even

[Expected Approach] Using Mathematical Observation - O(n) time and O(1) space

The idea is based on following mathematical calculation facts:

  1. Product of two even numbers is even.
  2. Product of two odd numbers is odd.
  3. 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++
// C++ program to Check whether product
// of 'n' numbers is even or odd
#include <bits/stdc++.h>
using namespace std;

bool isEven(vector<int> &arr) {
    for (int i = 0; i < arr.size(); i++) {
        if (arr[i] % 2 == 0) {
            return true;
        }
    }
    return false;
}

int main() {
    vector<int> arr = {2, 4, 3, 5};
    if (isEven(arr)) {
        cout << "Even" << endl;
    }
    else {
        cout << "Odd" << endl;
    }
    return 0;
}
Java
// Java program to Check whether product
// of 'n' numbers is even or odd

class GfG {

    static boolean isEven(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] % 2 == 0) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] arr = {2, 4, 3, 5};
        if (isEven(arr)) {
            System.out.println("Even");
        } else {
            System.out.println("Odd");
        }
    }
}
Python
# Python program to Check whether product
# of 'n' numbers is even or odd

def isEven(arr):
    for i in range(len(arr)):
        if arr[i] % 2 == 0:
            return True
    return False

if __name__ == "__main__":
    arr = [2, 4, 3, 5]
    if isEven(arr):
        print("Even")
    else:
        print("Odd")
C#
// C# program to Check whether product
// of 'n' numbers is even or odd

using System;

class GfG {

    static bool isEven(int[] arr) {
        for (int i = 0; i < arr.Length; i++) {
            if (arr[i] % 2 == 0) {
                return true;
            }
        }
        return false;
    }

    static void Main(string[] args) {
        int[] arr = {2, 4, 3, 5};
        if (isEven(arr)) {
            Console.WriteLine("Even");
        } else {
            Console.WriteLine("Odd");
        }
    }
}
JavaScript
// JavaScript program to Check whether product
// of 'n' numbers is even or odd

function isEven(arr) {
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] % 2 === 0) {
            return true;
        }
    }
    return false;
}

let arr = [2, 4, 3, 5];
if (isEven(arr)) {
    console.log("Even");
} else {
    console.log("Odd");
}

Output
Even

Similar Reads