2Brute2Best
🔥0

Subset Sum Problem

Medium·GeeksforGeeks Pro OnlyBasic Reinforcement
GeeksforGeeks
01

Problem Statement

You are given a list of positive whole numbers arr and a target sum. Decide whether some subset of the numbers adds up to exactly sum. Each number can be used at most once. The empty subset adds up to 0.

Return true if some subset adds up to sum, otherwise false. The function is bool isSubsetSum(vector<int>& arr, int sum).

Input

arr, a list of positive numbers. sum, the target.

Output

true if some subset of arr adds up to sum, otherwise false.

Example 1
Input: arr = [3, 34, 4, 12, 5, 2], sum = 9
Output: true

The numbers 4 and 5 add up to 9.

Example 2
Input: arr = [3, 34, 4, 12, 5, 2], sum = 30
Output: false

34 is too big. The other numbers add up to only 3 + 4 + 12 + 5 + 2 = 26, so 30 cannot be reached.

Example 3
Input: arr = [3], sum = 6
Output: false

There is only one 3. Using it twice (3 + 3) is not allowed.

Constraints
  • • 1 ≤ arr.size(), arr[i] ≤ 200
  • • 1 ≤ sum ≤ 10^4
02

Pattern Recognition Clues

Recognising these key signals in the problem description immediately identifies the pattern:

“some subset adds up to exactly sum”
→ Pick items so the total hits a target → Subset Sum, which is 0/1 knapsack where each value equals its weight.
“each number can be used at most once”
→ Each number is in or out, so the sum loop must go high → low.
“return true or false”
→ Only reachability matters, so each cell stores a boolean, not a best value.
03

Approach Complexity Ladder

Brute Force — skip or take every numberO(2^N)O(N)

Recurse on each number: skip it, or take it and lower the sum still needed

Better — 2-D table of true / falseO(N · S)O(N · S)

Store dp[i][s] for every prefix of the numbers and every sum; each cell is O(1)

Optimal — one row of bools, sum high → lowO(N · S)O(S)

Keep one row dp[s]; for each number, update s from sum down to that number

Brute Force Intuition

Go through the numbers one by one. For each one, skip it, or take it when it fits and lower the sum still needed. The answer is true if either branch reaches a sum of 0. Every subset is tried, with no memory between calls.

Recognition rule for this sub-pattern:

Each item once. dp[c] = max(dp[c], dp[c − w] + v), capacity loop goes high → low.
Remember this · DP — Knapsack
Trigger
Pick a subset to hit a target sum / capacity, partition into two groups, count the ways to reach an amount.
Think
Is this "choose some items so their total is X"?
Key question
Can each item be used once (loop capacity backwards) or many times (loop forwards)?
Brute2Best Pro Content

Unlock Complete Visual Walkthrough & Optimal C++

The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.

All 440 visual dry runs & algorithms
Interactive step-by-step playback with pointer & array state transitions
52 comprehensive LLD chapters
Interactive UML diagrams, real-world stories (Food Delivery, Trading, RPG), and practice solutions
40 C++ basics labs & hardware memory models
Vtables, byte layouts, assembly insights, and multi-threading race conditions
Production-ready C++23 source code
Modern idioms, clean contracts, time/space complexity derivations, and interview takeaways

First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.

Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like DP — Knapsack translate into production architectures and low-level memory mechanics.