Subset Sum

Given an array of positive integers and a target sum, determine whether there exists a subset whose sum equals the target. This is the classic subset-sum decision problem and a foundational knapsack transformation.

Input Format

nums = array of positive integers, target = required sum

Output Format

true if some subset sums to target, otherwise false

Constraints

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

Examples

Example 1:

Input:

nums = [3,34,4,12,5,2]
target = 9

Output:

true

Explanation:

A subset like 4 + 5 sums to 9.

Example 2:

Input:

nums = [1,2,3]
target = 7

Output:

false

Explanation:

No subset can reach 7.

Loading...
Subset Sum - Dp Knapsack DSA Problem