Find the Sum of the Power of All Subsequences - Problem

You are given an integer array nums of length n and a positive integer k. Your task is to find the sum of power of all subsequences of the array.

The power of any subsequence is defined as the number of its sub-subsequences that sum to k. For example, if a subsequence is [2, 3, 5] and k = 5, then its power is the count of all combinations from [2, 3, 5] that sum to 5 (which would be [2, 3] and [5], so power = 2).

Since the final answer can be extremely large, return it modulo 109 + 7.

Example: If nums = [1, 2, 3] and k = 3, we need to:

  • Find all possible subsequences of [1, 2, 3]
  • For each subsequence, count how many of its sub-subsequences sum to 3
  • Sum all these counts together

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1, 2, 3], k = 3
โ€บ Output: 6
๐Ÿ’ก Note: All subsequences: [1],[2],[3],[1,2],[1,3],[2,3],[1,2,3]. For [3]: sub-subsequences are [],[3] โ†’ power=1 (only [3] sums to 3). For [1,2]: sub-subsequences are [],[1],[2],[1,2] โ†’ power=1 (only [1,2] sums to 3). For [1,2,3]: 8 sub-subsequences, power=3 ([3],[1,2] sum to 3). Total = 1+1+3+1 = 6
example_2.py โ€” Small Values
$ Input: nums = [2, 3, 3], k = 5
โ€บ Output: 4
๐Ÿ’ก Note: Subsequence [2,3] appears twice (using first or second 3), each contributing 1 to power. Subsequence [2,3,3] has sub-subsequences that sum to 5: [2,3] (2 ways). Total power = 1+1+2 = 4
example_3.py โ€” Edge Case
$ Input: nums = [1], k = 1
โ€บ Output: 1
๐Ÿ’ก Note: Only one subsequence [1]. Its sub-subsequences are [] (sum=0) and [1] (sum=1). Only [1] sums to k=1, so power=1. Total=1

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 100
  • 1 โ‰ค k โ‰ค 100
  • Answer fits in 32-bit integer after modulo operation

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Treasure Hunt Committee ProblemOriginal Treasure Chests:๐Ÿ’ฐ1๐Ÿ’ฐ2๐Ÿ’ฐ3Target: k=3 coinsCommittees (Subsequences):๐Ÿ’ฐ1 ๐Ÿ’ฐ2๐Ÿ’ฐ3๐Ÿ’ฐ1 ๐Ÿ’ฐ2 ๐Ÿ’ฐ3...and moreTreasure Maps (Sub-subsequences):Committee [๐Ÿ’ฐ1 ๐Ÿ’ฐ2] creates maps:โˆ…๐Ÿ’ฐ1๐Ÿ’ฐ2๐Ÿ’ฐ1๐Ÿ’ฐ2โœ“ Sum=3Committee [๐Ÿ’ฐ3] creates maps:โˆ…๐Ÿ’ฐ3โœ“ Sum=3โšก DP Solution StrategyInstead of generating all committees and maps:1. Track how many ways each element contributes2. Use states: (length, sum) โ†’ count3. Three transitions per element:โ€ข Skip entirelyโ€ข Include in committee onlyโ€ข Include in committee AND mapsTime: O(nยฒk) vs O(4^n)โœ“ Efficient for large inputsโœ“ Mathematically elegantโœ“ Avoids exponential explosion๐ŸŽฏ Power CalculationCommittee [๐Ÿ’ฐ1 ๐Ÿ’ฐ2]: Power = 1 (one winning map)Committee [๐Ÿ’ฐ3]: Power = 1 (one winning map)Total Power = 1 + 1 + ... = Final Answer
Understanding the Visualization
1
Form Committees
Each subsequence is like a committee that decides which treasure chests to consider
2
Create Maps
Each committee creates all possible treasure maps from their chosen chests
3
Count Winning Maps
For each committee, count how many maps lead to exactly k coins
4
Sum All Powers
Add up the power (winning map count) from all committees
Key Takeaway
๐ŸŽฏ Key Insight: Use dynamic programming to count element contributions across all subsequences simultaneously, avoiding the exponential cost of explicit generation.
Asked in
Google 32 Meta 28 Amazon 24 Microsoft 18
28.4K Views
High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen