Find the Sum of the Power of All Subsequences
HardUpdated: Aug 2, 2025
Practice on:
Problem
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 thesum of power of all subsequences of nums .
Since the answer may be very large, return it modulo 10^9 + 7.
Examples
Example 1
Input: nums = [1,2,3], k = 3
Output: 6
Explanation:
There are `5` subsequences of nums with non-zero power:
* The subsequence `[_**1**_ ,_**2**_ ,_**3**_]` has `2` subsequences with `sum == 3`: `[1,2,_3_]` and `[_1_ ,_2_ ,3]`.
* The subsequence `[_**1**_ ,2,_**3**_]` has `1` subsequence with `sum == 3`: `[1,2,_3_]`.
* The subsequence `[1,_**2**_ ,_**3**_]` has `1` subsequence with `sum == 3`: `[1,2,_3_]`.
* The subsequence `[_**1**_ ,_**2**_ ,3]` has `1` subsequence with `sum == 3`: `[_1_ ,_2_ ,3]`.
* The subsequence `[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:
* The subsequence `[_**2**_ ,_**3**_ ,_**3**_]` has 2 subsequences with `sum == 5`: `[_2_ ,3,_3_]` and `[_2_ ,_3_ ,3]`.
* The subsequence `[_**2**_ ,3,_**3**_]` has 1 subsequence with `sum == 5`: `[_2_ ,3,_3_]`.
* The subsequence `[_**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] <= 10^41 <= k <= 100
Solution
Method 1 – Dynamic Programming (Subset Sum Counting)
Intuition
For each subsequence, its power is the number of subsequences whose sum is exactly k. The sum of the power of all subsequences is the total number of ways to pick any subset whose sum is k, for all possible subsets. This is a classic subset sum problem, and we can use dynamic programming to count the number of ways to reach each sum.
Approach
- Initialize a DP array
dpof size k+1, wheredp[j]is the number of subsequences with sum j. - Set
dp[0] = 1(empty subsequence). - For each number in nums, update dp from k down to num:
- For each j from k down to num, add
dp[j - num]todp[j].
- For each j from k down to num, add
- The answer is
dp[k].
Code
C++
class Solution {
public:
int sumOfPower(vector<int>& nums, int k) {
const int MOD = 1e9 + 7;
vector<int> dp(k + 1);
dp[0] = 1;
for (int x : nums) {
for (int j = k; j >= x; --j) {
dp[j] = (dp[j] + dp[j - x]) % MOD;
}
}
return dp[k];
}
};
Go
func sumOfPower(nums []int, k int) int {
mod := int(1e9 + 7)
dp := make([]int, k+1)
dp[0] = 1
for _, x := range nums {
for j := k; j >= x; j-- {
dp[j] = (dp[j] + dp[j-x]) % mod
}
}
return dp[k]
}
Java
class Solution {
public int sumOfPower(int[] nums, int k) {
int MOD = 1_000_000_007;
int[] dp = new int[k + 1];
dp[0] = 1;
for (int x : nums) {
for (int j = k; j >= x; --j) {
dp[j] = (dp[j] + dp[j - x]) % MOD;
}
}
return dp[k];
}
}
Kotlin
class Solution {
fun sumOfPower(nums: IntArray, k: Int): Int {
val MOD = 1_000_000_007
val dp = IntArray(k + 1)
dp[0] = 1
for (x in nums) {
for (j in k downTo x) {
dp[j] = (dp[j] + dp[j - x]) % MOD
}
}
return dp[k]
}
}
Python
class Solution:
def sumOfPower(self, nums: list[int], k: int) -> int:
MOD = 10**9 + 7
dp = [0] * (k + 1)
dp[0] = 1
for x in nums:
for j in range(k, x - 1, -1):
dp[j] = (dp[j] + dp[j - x]) % MOD
return dp[k]
Rust
impl Solution {
pub fn sum_of_power(nums: Vec<i32>, k: i32) -> i32 {
const MOD: i32 = 1_000_000_007;
let k = k as usize;
let mut dp = vec![0; k + 1];
dp[0] = 1;
for &x in &nums {
let x = x as usize;
for j in (x..=k).rev() {
dp[j] = (dp[j] + dp[j - x]) % MOD;
}
}
dp[k]
}
}
TypeScript
class Solution {
sumOfPower(nums: number[], k: number): number {
const MOD = 1e9 + 7;
const dp = Array(k + 1).fill(0);
dp[0] = 1;
for (const x of nums) {
for (let j = k; j >= x; --j) {
dp[j] = (dp[j] + dp[j - x]) % MOD;
}
}
return dp[k];
}
}
Complexity
- ⏰ Time complexity:
O(nk)— For each number, update up to k sums. - 🧺 Space complexity:
O(k)— For the DP array.