You are given two positive integers n and k.
A valid sequence is a sequence of k positive integers such that:
n.Return the number of valid sequences. Since the answer may be very large, return it modulo 109 + 7.
Two sequences are considered different if they differ at any index. For example, [1, 1, 2] and [1, 2, 1] are considered different sequences.
Example 1:
Input: n = 5, k = 3
Output: 3
Explanation:
The sequences of length k = 3 whose sum is 5 are:
| Sequence | Product | Parity |
|---|---|---|
[1, 1, 3] |
1 * 1 * 3 = 3 |
Odd |
[1, 2, 2] |
1 * 2 * 2 = 4 |
Even |
[2, 1, 2] |
2 * 1 * 2 = 4 |
Even |
[2, 2, 1] |
2 * 2 * 1 = 4 |
Even |
[1, 3, 1] |
1 * 3 * 1 = 3 |
Odd |
[3, 1, 1] |
3 * 1 * 1 = 3 |
Odd |
There are 3 sequences with an even product, thus the answer is 3.
Example 2:
Input: n = 3, k = 2
Output: 2
Explanation:
The sequences of length k = 2 whose sum is 3 are:
| Sequence | Product | Parity |
|---|---|---|
[1, 2] |
1 * 2 = 2 |
Even |
[2, 1] |
2 * 1 = 2 |
Even |
There are 2 sequences with an even product, thus the answer is 2.
Example 3:
Input: n = 5, k = 5
Output: 0
Explanation:
The only possible sequence of length k = 5 whose sum is 5 is [1, 1, 1, 1, 1], which has an odd product. Thus, the answer is 0.
Constraints:
1 <= n <= 5 * 1051 <= k <= nProblem Overview: You need to count how many sequences satisfy a set of validity rules while processing elements in order. Most solutions fail because they recompute the same states repeatedly or ignore how previous choices affect future positions.
Approach 1: Brute Force Backtracking (Exponential Time)
The direct solution generates every possible sequence and checks whether it satisfies the constraints. You recursively place each candidate value, validate the partial sequence, then continue to the next index. This approach is useful for understanding the state transitions and testing small inputs, but the branching factor grows quickly. Time complexity is O(k^n) in the worst case, and space complexity is O(n) from recursion depth.
Approach 2: Top-Down Dynamic Programming with Memoization (O(n * k))
Instead of recomputing the same subproblems, cache results using a memo table keyed by the current index and previous state. Each recursive call represents the number of valid continuations from that position. The key insight is that future choices depend only on a limited amount of prior information, which makes the problem a strong fit for dynamic programming. This reduces repeated work dramatically. Time complexity becomes O(n * k), while space complexity is O(n * k) for the cache and recursion stack.
Approach 3: Bottom-Up DP with Prefix Sum Optimization (O(n * k))
The optimal implementation builds the DP table iteratively instead of using recursion. For each position, compute the number of valid transitions from earlier states. If the transition range is large, maintain running totals using a prefix sum array so each state update becomes constant time instead of iterating over all previous values. This removes recursion overhead and performs better on large constraints. Time complexity stays O(n * k), but the constant factors are lower. Space complexity is O(k) when only the previous row is stored.
Approach 4: State Compression DP (O(n * k))
If the state contains multiple dimensions, you can compress it into a smaller representation using bitmasks or rolling arrays. This is common when the sequence rules depend on adjacent values or parity conditions. Combining compressed states with iterative transitions gives better cache locality and lower memory usage. This approach often appears alongside array-based DP optimizations in interview settings.
Recommended for interviews: Interviewers usually expect the dynamic programming solution with memoization or iterative tabulation. Showing the brute force approach first demonstrates that you understand the search space. Transitioning to cached states and prefix sum optimization shows that you can identify overlapping subproblems and reduce unnecessary computation.
Solutions for this problem are being prepared.
Try solving it yourself| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Backtracking | O(k^n) | O(n) | Small inputs or validating transition logic |
| Top-Down DP with Memoization | O(n * k) | O(n * k) | General case with overlapping subproblems |
| Bottom-Up DP + Prefix Sums | O(n * k) | O(k) | Large constraints and performance-sensitive cases |
| State Compression DP | O(n * k) | O(k) | Complex state transitions with memory constraints |
Practice Count Valid Sequences with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor