Skip to content
Onsite

Subset Sum

MediumP2Coding

Given an array of positive integers nums and a non-negative integer target, return true if some subset of nums (each element used at most once) adds up to exactly target. The empty subset has sum 0.

Example 1

Input
nums = [3,34,4,12,5,2], target = 9
Output
true
Why
4 + 5 = 9.

Example 2

Input
nums = [3,34,4,12,5,2], target = 30
Output
false

Example 3

Input
nums = [], target = 0
Output
true

Constraints

  • 0 <= nums.length <= 200
  • 1 <= nums[i] <= 1000
  • 0 <= target <= 10^4

Hints

Run checks the 3 visible tests, Submit all 12.