Subset Sum Problem
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).
arr, a list of positive numbers. sum, the target.
true if some subset of arr adds up to sum, otherwise false.
The numbers 4 and 5 add up to 9.
34 is too big. The other numbers add up to only 3 + 4 + 12 + 5 + 2 = 26, so 30 cannot be reached.
There is only one 3. Using it twice (3 + 3) is not allowed.
- • 1 ≤ arr.size(), arr[i] ≤ 200
- • 1 ≤ sum ≤ 10^4
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse on each number: skip it, or take it and lower the sum still needed
Store dp[i][s] for every prefix of the numbers and every sum; each cell is O(1)
Keep one row dp[s]; for each number, update s from sum down to that number
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:
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.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like DP — Knapsack translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.