Problem · Array
Positive Subset Sum
Learn this problemProblem statement
You are given an array nums of positive integers and a positive integer target. Return true when some subset of array positions sums exactly to target; otherwise return false.
Each array position may be selected at most once. Equal values at different positions are separate choices.
As an interview follow-up, explain how to reconstruct and return one concrete subset after determining that the target is reachable.
Function
canReachTarget(nums: int[], target: int) → booleanExamples
Example 1
nums = [12,1,61,5,9,2]target = 24return = trueThe positions containing 12, 9, 2, and 1 sum to 24.
Example 2
nums = [2,4,6]target = 5return = falseNo subset of the three positive values sums to 5.
Example 3
nums = [5,5,11]target = 10return = trueThe two occurrences of 5 occupy different positions and may both be selected.
Constraints
1 <= nums.length <= 200.1 <= nums[i] <= 10000.1 <= target <= 20000.