Number of Subarrays Having Even Product - Solution & Explanation
Problem Statement
Given a 0-indexed integer array nums, return the number of subarrays of nums having an even product.
Example 1:
Input: nums = [9,6,7,13] Output: 6 Explanation: There are 6 subarrays with an even product: - nums[0..1] = 9 * 6 = 54. - nums[0..2] = 9 * 6 * 7 = 378. - nums[0..3] = 9 * 6 * 7 * 13 = 4914. - nums[1..1] = 6. - nums[1..2] = 6 * 7 = 42. - nums[1..3] = 6 * 7 * 13 = 546.
Example 2:
Input: nums = [7,3,5] Output: 0 Explanation: There are no subarrays with an even product.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 105
Approach Overview
Problem Overview: Given an integer array, count how many subarrays have an even product. A product becomes even as soon as the subarray contains at least one even number. The task is to efficiently count all such subarrays without explicitly computing every product.
Approach 1: Brute Force Enumeration (O(n2) time, O(1) space)
Generate every possible subarray using two nested loops. For each starting index i, extend the subarray to every j ≥ i and maintain the running product or simply check if any element in the window is even. Once an even number appears, every further extension of that subarray will also have an even product. This approach is straightforward but inefficient for large arrays because it inspects O(n2) subarrays.
Approach 2: Single Pass Counting (O(n) time, O(1) space)
The key observation: a subarray has an even product if it contains at least one even number. Instead of counting even-product subarrays directly, track how many subarrays consist entirely of odd numbers. While iterating through the array, maintain the length of the current streak of odd values. If the current element is odd, extend the streak and add it to the count of odd-only subarrays. When an even number appears, the streak resets to zero.
After processing the array, compute the total number of subarrays using n * (n + 1) / 2. Subtract the number of all-odd subarrays from this total. The remainder represents subarrays containing at least one even number, which guarantees an even product. This works because parity determines the product’s parity; multiplication with any even value always produces an even result.
This approach scans the array once and uses only a few counters, giving linear performance. It relies purely on parity checks rather than multiplication, making it both faster and numerically safe. The logic fits naturally with common array iteration patterns and leverages a simple observation from math about even and odd numbers. The incremental counting pattern also resembles techniques used in lightweight dynamic programming where a running state summarizes previous elements.
Recommended for interviews: Interviewers expect the single-pass approach. Brute force demonstrates you understand the problem space, but recognizing the parity property and counting odd-only segments shows stronger problem-solving skills and leads to the optimal O(n) solution.
Solution
We know that the product of a subarray is even if and only if there is at least one even number in the subarray.
Therefore, we can traverse the array, record the index last of the most recent even number, then the number of subarrays ending with the current element and having an even product is last + 1. We can add this to the result.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Subarray Enumeration | O(n²) | O(1) | Small arrays or when first exploring the problem during interviews |
| Single Pass Odd-Streak Counting | O(n) | O(1) | General case and optimal solution expected in coding interviews |
Video Solution
leetcode 2495. Number of Subarrays Having Even Product - check last seen even number's index • Code-Yao • 176 views views
Watch 3 more video solutions →Frequently Asked Questions
Is Number of Subarrays Having Even Product easy or hard?
Number of Subarrays Having Even Product Python/Java solution
How to solve Number of Subarrays Having Even Product in O(n)?
What is the best approach for Number of Subarrays Having Even Product?
Is Number of Subarrays Having Even Product asked at Google/Amazon/Meta?
What data structure is used in Number of Subarrays Having Even Product?
What is the time complexity of Number of Subarrays Having Even Product?
Ready to solve this problem?
Practice Number of Subarrays Having Even Product with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor