Coin Change II
Problem Statement
You are given coins of several different values and a total amount. You have an unlimited supply of every coin.
Count how many different combinations of coins add up to exactly amount. Two combinations are different when they use a different number of some coin.
The order of the coins does not matter: 2 + 1 and 1 + 2 are the same combination. If the amount cannot be made, the answer is 0.
amount, the total to make, and coins, a list of different coin values.
The number of combinations of coins that add up to amount.
The four combinations are 5; 2 + 2 + 1; 2 + 1 + 1 + 1; and 1 + 1 + 1 + 1 + 1.
Only 2s are available, so 3 cannot be made.
A single coin of 10 is the only combination.
- • 1 ≤ coins.length ≤ 300
- • 1 ≤ coins[i] ≤ 5000
- • All the values of coins are unique.
- • 0 ≤ amount ≤ 5000
- • The answer is guaranteed to fit in a signed 32-bit integer.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Recurse on (coin index, amount left): skip the coin, or use it once more and stay on it
ways[i][a] = combinations using the first i coin types; each cell is worked out once
One array ways[a]; for each coin type, go over the amounts low → high and add ways[a − coin]
Go through the coin types in order. For each one, either skip it and move on, or use one more copy and stay on the same coin, so it can repeat. Each path that reaches 0 is one combination. Coins are only taken in list order, so every combination is found once.
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.