![]() |
VOOZH | about |
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.
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.
Even
The idea 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.
Even