---
# System prepended metadata

title: 2495. Number of Subarrays Having Even Product
tags: [Leetcode, Dynamic Programming, Medium]

---

# 2495. Number of Subarrays Having Even Product
###### tags: `Leetcode` `Medium` `Dynamic Programming`
Link: https://leetcode.com/problems/number-of-subarrays-having-even-product/description/
## 思路
count by iterating right boundary
`val`表示以当前数字结尾的valid subarray数目
如果`num`是偶数 那么`val = i+1`
否则 `val`不变
## Code
```python=
class Solution:
    def evenProduct(self, nums: List[int]) -> int:
        ans = 0
        val = 0
        for i, num in enumerate(nums):
            if num%2==0: val = i+1
            ans += val
        return ans
```
```java=
class Solution {
    public long evenProduct(int[] nums) {
        long ans = 0;
        int n = nums.length;
        int val = 0;
        for(int i=0; i<n; i++){
            if(nums[i]%2==0) val = i+1;
            ans += val;
        }
        return ans;
    }
}
```