Number of Subsequences with Odd Sum - Problem
Given an array nums, return the number of subsequences with an odd sum of elements.
A subsequence is a sequence that can be derived from the array by deleting some or no elements without changing the order of the remaining elements.
Since the answer may be very large, return it modulo 109 + 7.
Input & Output
Example 1 — All Odd Numbers
$
Input:
nums = [1,3,5]
›
Output:
4
💡 Note:
The subsequences with odd sums are [1], [3], [5], [1,3,5]. Sums are 1, 3, 5, 9 respectively - all odd. Total count = 4.
Example 2 — Mixed Odd and Even
$
Input:
nums = [2,4,6]
›
Output:
0
💡 Note:
All numbers are even, so any sum will be even. No subsequences can have odd sums. Total count = 0.
Example 3 — Single Odd Element
$
Input:
nums = [1,2,4]
›
Output:
4
💡 Note:
Subsequences with odd sums: [1] (sum=1), [1,2] (sum=3), [1,4] (sum=5), [1,2,4] (sum=7). All contain the single odd number 1. Total count = 4.
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i] ≤ 109
- Answer is returned modulo 109 + 7
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array with mix of odd and even numbers
2
Key Insight
Odd sum requires odd number of odd elements
3
Count Result
Use combinatorics to count valid subsequences
Key Takeaway
🎯 Key Insight: Odd sums require an odd number of odd elements - use combinatorics to count efficiently
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code