Check if there exists a subsequence with sum K
Pattern: Subsequences with sum
Carry the sum still needed. At each value, either take it (subtract it) or skip it. Reaching exactly 0 means yes; with positive values, going below 0 ends that branch.
The problem
Given an array of positive whole numbers and a target k, return whether some subsequence (any subset of the elements)
adds up to exactly k. For example, [1, 2, 3, 4, 5] with k = 8 gives true (for example 1 + 3 + 4), and
[4, 3, 9, 2] with k = 10 gives false.
The function name checkSubsequenceSum matches the takeUforward practice page. Use the link above for the exact
statement and limits. The values are positive.
Think first
- What two choices does each element have?
- What should the recursion carry instead of the chosen values?
- When can a branch give up early?
Hints
Brute force, and why it fails
List every subset and add it up: O(2ⁿ × n). The recursion is O(2ⁿ) and usually stops much earlier. A table of reachable sums (Subset Sum Problem) is O(n × k).
The key insight
Only the remaining amount matters, not which values were chosen. That makes the state small, and positive values let overshooting branches die at once.
Dry run
| left | 8 |
|---|
nums = [1, 2, 3, 4, 5] and k = 8. Start with 8 still needed.
The plan
function find(i, left):
if left == 0: return true
if i == n or left < 0: return false
return find(i + 1, left - a[i]) or find(i + 1, left)
return find(0, k)
Complexity
- Time
O(2ⁿ)- Space
O(n)
At most two branches per element, and the recursion is n deep. The early stops make typical cases faster.
Edge cases
- One element equal to
k: true. klarger than the total: false.- Many equal values: still correct, though the search may be slow; use the table version then.
I've built my logic, show the code
Java
class Solution {
public boolean checkSubsequenceSum(int[] nums, int k) {
return find(nums, 0, k);
}
private boolean find(int[] nums, int i, int left) {
if (left == 0) return true; // the chosen values add up exactly (values are positive)
if (i == nums.length || left < 0) return false;
return find(nums, i + 1, left - nums[i]) // take nums[i]
|| find(nums, i + 1, left); // or skip it
}
}C++
class Solution {
public:
bool checkSubsequenceSum(vector<int>& nums, int k) {
return find(nums, 0, k);
}
bool find(vector<int>& nums, size_t i, int left) {
if (left == 0) return true; // the chosen values add up exactly (values are positive)
if (i == nums.size() || left < 0) return false;
return find(nums, i + 1, left - nums[i]) // take nums[i]
|| find(nums, i + 1, left); // or skip it
}
};Python
class Solution:
def checkSubsequenceSum(self, nums: list[int], k: int) -> bool:
def find(i: int, left: int) -> bool:
if left == 0:
return True # the chosen values add up exactly (values are positive)
if i == len(nums) or left < 0:
return False
return find(i + 1, left - nums[i]) or find(i + 1, left) # take nums[i], or skip it
return find(0, k)Common mistakes
- Pruning on
left < 0when values can be negative. - Checking
i == nbeforeleft == 0, which misses a sum completed by the last element. - Continuing the search after finding one subsequence.