Skip to main content

Practice · 5 of 5

Subset sum

CHALLENGE
Difficulty: advanced+25 XP

Write hasSum(int[] arr, int target) returning true if **some subset** of the values sums to exactly target (each value used at most once). Use recursion over the index: at each element, either include it (target - value) or skip it. Empty subset sums to 0, so target 0 is always true. Examples: {2,7,9}, target 11 → true (2+9); target 5 → false.

Back to lesson: Practice: Recursion gauntlet