Combination Sum IV
Problem Statement
You get a list of distinct positive numbers nums and a number target. Count how many sequences of numbers from nums add up to target. You may use each number as many times as you like.
The order matters here: 1 + 2 and 2 + 1 are two different answers. Return the count.
nums, a list of distinct positive integers, and target, the sum to reach.
The number of ordered sequences of numbers from nums whose sum is target.
The sequences are 1+1+1+1, 1+1+2, 1+2+1, 1+3, 2+1+1, 2+2 and 3+1. Sequences like 1+2+1 and 2+1+1 count as different answers.
9 is bigger than 3, so no sequence can make 3.
- • 1 ≤ nums.length ≤ 200
- • 1 ≤ nums[i] ≤ 1000
- • All the elements of nums are unique.
- • 1 ≤ target ≤ 1000
- • The test cases are generated so that the answer can fit in a 32-bit integer.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Count sequences by recursion: for each number that fits, count the sequences for what is left
ways[t] = sum of ways[t − x] over every number x, filled for t = 1 … target
To count the sequences that make left, look at the last number of each sequence. For each x ≤ left, count the sequences that make left − x and add them up. The empty sequence makes 0 in exactly one way.
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.