Skip to main content

Number of Subsequences with Odd Sum - Solution & Explanation

MediumPremiumFree on FleetCodeArrayMathDynamic ProgrammingCombinatorics12 min read
Practice this problem

Problem Statement

Given an array nums, return the number of subsequences with an odd sum of elements.

Since the answer may be very large, return it modulo 109 + 7.

 

Example 1:

Input: nums = [1,1,1]

Output: 4

Explanation:

The odd-sum subsequences are: [1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1].

Example 2:

Input: nums = [1,2,2]

Output: 4

Explanation:

The odd-sum subsequences are: [1, 2, 2], [1, 2, 2], [1, 2, 2], [1, 2, 2].

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Approach Overview

Problem Overview: Given an integer array, count how many subsequences have an odd sum. A subsequence can skip elements but must keep the original order. The key observation is that the parity of the sum (odd or even) depends only on how many odd numbers are included.

Approach 1: Brute Force Enumeration (O(2^n) time, O(1) space)

Generate every possible subsequence and compute its sum. A bitmask from 0 to 2^n - 1 represents which elements are included. For each mask, iterate through the array, accumulate the sum, and check whether the result is odd. This approach demonstrates the definition of subsequences clearly but becomes impractical once n grows beyond ~20 because the number of subsets doubles with each element.

Approach 2: Dynamic Programming with Parity Tracking (O(n) time, O(1) space)

Track two values while iterating through the array: the number of subsequences with even sum and the number with odd sum. When you see an even number, appending it keeps the parity unchanged. When you see an odd number, appending it flips the parity. Update counts accordingly: new odd subsequences come from extending previous even ones with the odd element, while new even subsequences come from extending previous odd ones. This is a classic dynamic programming pattern where the state only depends on the previous step. The algorithm processes each element once and maintains constant memory.

Approach 3: Combinatorics / Parity Counting (O(n) time, O(1) space)

A subsequence has an odd sum if it contains an odd number of odd elements. Count how many numbers are even (e) and how many are odd (o). Any subset of even numbers can be chosen freely (2^e choices). For odd numbers, you must choose an odd-sized subset, which has 2^(o-1) possibilities when o > 0. Multiply these counts to get the total number of valid subsequences. This reasoning relies on simple combinatorics and parity properties rather than iterative state updates.

Recommended for interviews: The dynamic programming approach is what interviewers typically expect. It shows you understand parity transitions and can model the problem with minimal state. Starting with brute force demonstrates baseline reasoning, but reducing it to an O(n) parity-based DP signals strong algorithmic intuition.

Solution

We define f[0] to represent the number of subsequences with an even sum so far, and f[1] to represent the number of subsequences with an odd sum so far. Initially, f[0] = 0 and f[1] = 0.

Traverse the array nums, for each number x:

If x is odd, the update rules for f[0] and f[1] are:

$ \begin{aligned} f[0] & = (f[0] + f[1]) bmod 10^9 + 7, \ f[1] & = (f[0] + f[1] + 1) bmod 10^9 + 7. \end{aligned}

That is, the current number of subsequences with an even sum is equal to the previous number of subsequences with an even sum plus the number of subsequences with an odd sum concatenated with the current number x; the current number of subsequences with an odd sum is equal to the previous number of subsequences with an even sum concatenated with the current number x plus the previous number of subsequences with an odd sum, plus one subsequence containing only the current number x.

If x is even, the update rules for f[0] and f[1] are:

\begin{aligned} f[0] & = (f[0] + f[0] + 1) bmod 10^9 + 7, \ f[1] & = (f[1] + f[1]) bmod 10^9 + 7. \end{aligned}

That is, the current number of subsequences with an even sum is equal to the previous number of subsequences with an even sum plus the number of subsequences with an even sum concatenated with the current number x, plus one subsequence containing only the current number x; the current number of subsequences with an odd sum is equal to the previous number of subsequences with an odd sum concatenated with the current number x plus the previous number of subsequences with an odd sum.

Finally, return f[1].

The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1)$.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force EnumerationO(2^n)O(1)For understanding subsequence generation or when n is very small
Dynamic Programming (Parity Tracking)O(n)O(1)General case and most common interview solution
Combinatorics CountingO(n)O(1)When you recognize the odd/even parity pattern and want a mathematical shortcut

Video Solution

Leetcode 1498 - Number of Subsequences That Satisfy the Given Sum Condition - Python • NeetCode • 32,519 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Number of Subsequences with Odd Sum easy or hard?
The problem is typically rated Medium. The brute force idea is straightforward, but recognizing the parity relationship and converting it into an O(n) dynamic programming or combinatorics solution requires deeper insight.
Number of Subsequences with Odd Sum Python/Java solution
The standard implementation keeps two variables: evenCount and oddCount. For each element, update them based on parity transitions. The same logic works across Python, Java, C++, Go, and TypeScript since it relies only on integer arithmetic and a single array traversal.
How to solve Number of Subsequences with Odd Sum in O(n)?
Iterate through the array while tracking counts of subsequences with even and odd sums. Adding an even number keeps the parity unchanged, while adding an odd number flips it. Update the counts based on these transitions for each element. This single-pass dynamic programming approach achieves O(n) time and constant space.
What is the best approach for Number of Subsequences with Odd Sum?
The optimal approach uses dynamic programming with parity tracking. Maintain two counters: the number of subsequences with even sums and the number with odd sums. When processing each element, update these counts depending on whether the number is even or odd. This solution runs in O(n) time and O(1) space.
Is Number of Subsequences with Odd Sum asked at Google/Amazon/Meta?
Parity and subsequence counting problems frequently appear in interviews at companies like Amazon, Google, and Meta. Variants involving odd/even sums, subset parity, or combinatorial counting are common in coding rounds because they test dynamic programming and mathematical reasoning.
What data structure is used in Number of Subsequences with Odd Sum?
The problem mainly uses simple counters rather than complex data structures. The core technique is dynamic programming with two variables representing counts of even-sum and odd-sum subsequences while iterating through the array.
What is the time complexity of Number of Subsequences with Odd Sum?
The optimal solution runs in O(n) time because the array is processed once while updating two parity counters. Space complexity is O(1) since only a few integer variables are stored regardless of input size.

Ready to solve this problem?

Practice Number of Subsequences with Odd Sum with our built-in code editor and test cases.

Practice on FleetCode