Problem 363909 · easy · Phase 03 Linear Management & Searching

Windows With an Even Product

counting · complement · runs

Count the non-empty contiguous windows of nums whose product is even. The product itself may be astronomically large, so you must not compute it.

Examples

Input:  nums = [1, 2, 3]
Output: 4
Explanation: [2], [1, 2], [2, 3], [1, 2, 3] are even; [1], [3] are odd.

Input:  nums = [1, 3, 5]
Output: 0

Constraints

  • 0 <= len(nums) <= 10**5
  • -10**9 <= nums[i] <= 10**9 (0 counts as even)
  • Target complexity: O(n) time.

Goals

  • Count the complement (all-odd windows) instead of the target directly
  • Turn a run of length L into L*(L+1)/2 windows
Starting Python…