Problems178
Search for a command to run...
Does any subsequence add up to the target?
Initialization
At each index, branch twice — take the element (subtract it from k) or skip it. Because the branches are OR-ed, the search stops the moment one path reaches 0.
Take / Skip Recursion
1function solve(i, n, arr, k) {2 // Target met3 if (k === 0) return true;4 5 // Overshot6 if (k < 0) return false;7 8 // Ran out of elements9 if (i === n) return k === 0;10 11 // Take arr[i], or skip it12 return solve(i + 1, n, arr, k - arr[i]) ||13 solve(i + 1, n, arr, k);14}15 16function checkSubsequenceSum(nums, target) {17 const n = nums.length;18 return solve(0, n, nums, target);//n: 3target: 319}