VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-whether-product-of-n-numbers-is-even-or-odd/

⇱ Check whether product of 'n' numbers is even or odd - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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

Last Updated : 6 May, 2025

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.


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.


Output
Even
Comment