Count Parity-Alternating Subsequences
Dynamic ProgrammingAuto-graded · 22 test casesLast updated
Tests dynamic programming with state tracking to count subsequences where consecutive elements alternate between even and odd values.
You are given an array of integers. Your task is to count how many non-empty subsequences exist where every pair of consecutive elements has opposite parity (one even, one odd).
A subsequence is formed by selecting elements from the array in their original order, possibly skipping some elements. For a subsequence to be valid, whenever two elements appear consecutively in the subsequence, one must be even and the other must be odd.
Single-element subsequences are always valid since they have no consecutive pairs.
Implement a function that counts all such valid subsequences. Since the count can be extremely large, return the result modulo 10^9 + 7.
Constraints
1 <= len(nums) <= 10^50 <= nums[i] <= 10^9- Subsequences are distinguished by the positions of their elements, not their values
- The result must be computed modulo
10^9 + 7
Examples
Example 1
Input: count_valid_subsequences([1, 2, 3])
Output: 6
Example 2
Input: count_valid_subsequences([2, 4, 6])
Output: 3
Hints
Hint 1
Track the count of valid subsequences based on the parity of their last element
Hint 2
When processing a new element, consider both starting a new single-element subsequence and extending existing subsequences with opposite parity
Hint 3
The state only depends on aggregate counts, not individual subsequence details, allowing constant space optimization
Worked solution
Hard Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is to maintain running counts of valid subsequences based on the parity of their final element. As we scan through the array, we track:
even_ending: count of valid subsequences ending with an even numberodd_ending: count of valid subsequences ending with an odd number
When we encounter a new element, we can:
- Start a fresh single-element subsequence (contributes 1)
- Extend any existing subsequence whose last element has opposite parity
For an even element, we can only extend subsequences ending in odd numbers (since odd + even = odd, maintaining the alternation). Similarly, an odd element can only extend subsequences ending in even numbers.
The crucial observation is that all previously counted subsequences remain valid even if they don't include the current element. This is implicitly handled because we're adding to the count, not replacing it.
Why This Works
Consider classifying subsequences by their last element's position. Each subsequence appears exactly once in this classification. When processing element at index i, we count:
- The single-element subsequence
[nums[i]] - All ways to extend valid subsequences from earlier positions with opposite parity
This counts each valid subsequence exactly once when we reach its final element. The recurrence correctly captures both cases: subsequences that end at the current position, and those that ended earlier (carried forward in the running totals).
Complexity
Time: O(n) - single pass through the array with constant work per element
Space: O(1) - only two integer counters needed
Implementation
from typing import List
MOD = 10**9 + 7
def count_valid_subsequences(nums: List[int]) -> int:
even_ending = 0
odd_ending = 0
for num in nums:
if num % 2 == 0:
even_ending = (even_ending + odd_ending + 1) % MOD
else:
odd_ending = (odd_ending + even_ending + 1) % MOD
return (even_ending + odd_ending) % MOD