Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given an integer array nums of length n and a positive integer k.
The power of an array of integers is defined as the number of subsequences with their sum equal to k.
Return the sum of power of all subsequences of nums.
Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: nums = [1,2,3], k = 3
Output: 6
Explanation:
There are 5 subsequences of nums with non-zero power:
[1,2,3] has 2 subsequences with sum == 3: [1,2,3] and [1,2,3].[1,2,3] has 1 subsequence with sum == 3: [1,2,3].[1,2,3] has 1 subsequence with sum == 3: [1,2,3].[1,2,3] has 1 subsequence with sum == 3: [1,2,3].[1,2,3] has 1 subsequence with sum == 3: [1,2,3].Hence the answer is 2 + 1 + 1 + 1 + 1 = 6.
Example 2:
Input: nums = [2,3,3], k = 5
Output: 4
Explanation:
There are 3 subsequences of nums with non-zero power:
[2,3,3] has 2 subsequences with sum == 5: [2,3,3] and [2,3,3].[2,3,3] has 1 subsequence with sum == 5: [2,3,3].[2,3,3] has 1 subsequence with sum == 5: [2,3,3].Hence the answer is 2 + 1 + 1 = 4.
Example 3:
Input: nums = [1,2,3], k = 7
Output: 0
Explanation: There exists no subsequence with sum 7. Hence all subsequences of nums have power = 0.
Constraints:
1 <= n <= 1001 <= nums[i] <= 1041 <= k <= 100Problem summary: You are given an integer array nums of length n and a positive integer k. The power of an array of integers is defined as the number of subsequences with their sum equal to k. Return the sum of power of all subsequences of nums. Since the answer may be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,2,3] 3
[2,3,3] 5
[1,2,3] 7
number-of-subsequences-that-satisfy-the-given-sum-condition)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3082: Find the Sum of the Power of All Subsequences
class Solution {
public int sumOfPower(int[] nums, int k) {
final int mod = (int) 1e9 + 7;
int n = nums.length;
int[][] f = new int[n + 1][k + 1];
f[0][0] = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= k; ++j) {
f[i][j] = (f[i - 1][j] * 2) % mod;
if (j >= nums[i - 1]) {
f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod;
}
}
}
return f[n][k];
}
}
// Accepted solution for LeetCode #3082: Find the Sum of the Power of All Subsequences
func sumOfPower(nums []int, k int) int {
const mod int = 1e9 + 7
n := len(nums)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, k+1)
}
f[0][0] = 1
for i := 1; i <= n; i++ {
for j := 0; j <= k; j++ {
f[i][j] = (f[i-1][j] * 2) % mod
if j >= nums[i-1] {
f[i][j] = (f[i][j] + f[i-1][j-nums[i-1]]) % mod
}
}
}
return f[n][k]
}
# Accepted solution for LeetCode #3082: Find the Sum of the Power of All Subsequences
class Solution:
def sumOfPower(self, nums: List[int], k: int) -> int:
mod = 10**9 + 7
n = len(nums)
f = [[0] * (k + 1) for _ in range(n + 1)]
f[0][0] = 1
for i, x in enumerate(nums, 1):
for j in range(k + 1):
f[i][j] = f[i - 1][j] * 2 % mod
if j >= x:
f[i][j] = (f[i][j] + f[i - 1][j - x]) % mod
return f[n][k]
// Accepted solution for LeetCode #3082: Find the Sum of the Power of All Subsequences
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3082: Find the Sum of the Power of All Subsequences
// class Solution {
// public int sumOfPower(int[] nums, int k) {
// final int mod = (int) 1e9 + 7;
// int n = nums.length;
// int[][] f = new int[n + 1][k + 1];
// f[0][0] = 1;
// for (int i = 1; i <= n; ++i) {
// for (int j = 0; j <= k; ++j) {
// f[i][j] = (f[i - 1][j] * 2) % mod;
// if (j >= nums[i - 1]) {
// f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod;
// }
// }
// }
// return f[n][k];
// }
// }
// Accepted solution for LeetCode #3082: Find the Sum of the Power of All Subsequences
function sumOfPower(nums: number[], k: number): number {
const mod = 10 ** 9 + 7;
const n = nums.length;
const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0));
f[0][0] = 1;
for (let i = 1; i <= n; ++i) {
for (let j = 0; j <= k; ++j) {
f[i][j] = (f[i - 1][j] * 2) % mod;
if (j >= nums[i - 1]) {
f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod;
}
}
}
return f[n][k];
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.