← Questions

Subset sum to a target

0
UberAppleCodingLast asked Jul 2026

Given a list of positive integers nums and a non-negative integer target, decide whether some subset of nums adds up to exactly target. Each element may be used at most once, though equal values at different positions count as separate elements.

Implement:

def can_reach_target(nums: list[int], target: int) -> bool: ...

Return True if such a subset exists and False otherwise. The empty subset sums to 0, so target = 0 is always reachable.

Constraints

  • 0 <= len(nums) <= 200
  • 1 <= nums[i] <= 1000
  • 0 <= target <= 10^5

Example 1

nums = [4, 9, 2, 7], target = 13
-> True   (4 + 9, or 4 + 2 + 7)

Example 2

nums = [5], target = 10
-> False  (the single 5 cannot be used twice)

Be ready to return one subset that reaches the target, not just a yes/no answer.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding