Menu
CoddyTech

Subset Sum Problem

nums holds positive integers, and target is a positive integer. Can you choose some of the values, each position at most once, so that they sum to exactly target? Return true when you can and false when no choice works.

Function

isSubsetSum(nums: integer-array, target: integer) → boolean
numsinteger-array
the positive values you may pick from
targetinteger
the exact sum the picked values must reach
Returnsboolean
true when some of the values add up to exactly target, false otherwise

Constraints

  • 1 ≤ nums.length ≤ 200
  • 1 ≤ nums[i] ≤ 104
  • 1 ≤ target ≤ 104

Examples

Input
nums = [8, 3, 11, 5, 7]target = 15
Output
true
Explanation
8 + 7 = 15, so the answer is true. The pick 3 + 5 + 7 works too; one pick is enough.
challenge icon

Follow-up

Two equal values at different positions count as different picks. How many picks reach target?

Reset code
def isSubsetSum(nums, target):
    # Write code here

Test #1

Test #2

Test #3

Arguments

nums = [8, 3, 11, 5, 7]
target = 15

Expected Output

true