Number of Subarrays Having Even Product - Problem
Given a 0-indexed integer array nums, return the number of subarrays of nums having an even product.
A subarray is a contiguous non-empty sequence of elements within an array. The product of a subarray is even if it contains at least one even number.
Input & Output
Example 1 — Mixed Even/Odd
$
Input:
nums = [2,1,4,3,6]
›
Output:
13
💡 Note:
Total subarrays: 15. Odd-only subarrays: [1] and [3] = 2. Even product subarrays: 15 - 2 = 13
Example 2 — All Odd
$
Input:
nums = [1,3,5]
›
Output:
0
💡 Note:
All numbers are odd, so no subarray can have an even product. Result is 0
Example 3 — All Even
$
Input:
nums = [2,4,6]
›
Output:
6
💡 Note:
All subarrays contain at least one even number, so all 6 subarrays have even product
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Array with mix of even and odd numbers
2
Identify Pattern
Find odd-only segments vs subarrays containing even numbers
3
Count Result
Total subarrays minus odd-only subarrays
Key Takeaway
🎯 Key Insight: Count what you DON'T want (odd-only subarrays) and subtract from total
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code