Introduction to Dynamic Programming: State, Recurrence and a Worked Coin-Change Example
Learn how to design a DP solution from zero, then apply the method to an unlimited coin-change problem using recursion, tabulation and answer reconstruction.
KnowledgeGate Team
Exam prep & CS education

Recognising a Fibonacci-style recurrence is not enough to solve a new dynamic programming problem. You may still be unsure what a state means, which transition is valid, or which states must be computed first. Dynamic programming can be designed through five decisions: state, transition, base case, evaluation order and answer location. The minimum-coin problem has both recursive and tabular solutions. Dynamic programming is a problem-solving method, not one fixed algorithm.
Dynamic programming basics: repeated subproblems plus reusable answers
Dynamic programming models a problem as states. It stores each solved state's answer and reuses it when the state returns. Overlapping subproblems occur when different recursive paths request the same state. Optimal substructure means an optimal answer can be built from optimal answers to smaller states.
The structure matters. Divide and conquer, as in merge sort, creates mostly independent subproblems. A greedy algorithm commits to one locally preferred choice, so it needs proof that the commitment is safe. DP instead keeps the best result for every relevant state before combining results. The contrast is developed further in Greedy Algorithms: Strategy and Problems.
The Dynamic Programming in Algorithms: Complete Guide with Worked Examples for GATE and Interviews uses a two-dimensional item-by-capacity state for 0/1 knapsack. Unlimited coin change instead needs only the remaining amount, and pick[x] reconstructs the selected coins.
Use unlimited coins {1, 3, 4} to make amount 6 with the fewest coins. A direct recursive function solve(x) repeatedly requests amounts such as 3, 2 and 1. The remaining amount is therefore a natural state.
Dynamic programming design: state, transition, base case, order and answer
A complete DP design makes five decisions explicit:
State:
dp[x]is the minimum number of coins needed to make amountx.Transition: choose the final coin
c, then solve the remaining amountx-c.Base and invalid states:
dp[0]=0, whiledp[x]=infinityforx<0.Evaluation order: compute smaller non-negative amounts before larger ones.
Answer location: the required result is
dp[6].
For x>0, the recurrence is dp[x] = 1 + min(dp[x-1], dp[x-3], dp[x-4]), ignoring terms with negative indices. The added 1 counts the final coin selected. A recurrence is incomplete until its state meaning and base cases are stated.
This table gives only the minimum count. To recover the actual coins, also store pick[x], the coin that attained the minimum. If two choices tie, either is valid as long as the optimal value remains correct.
Memoization and tabulation: two evaluation orders for the same recurrence
Top-down memoization begins with solve(6). It returns 0 at amount 0, infinity for a negative amount, and a cached value when x has already been solved. Otherwise, it tries coins 1, 3 and 4, caches the minimum result, and returns it. The first requested answer controls which reachable states are explored.
Bottom-up tabulation starts with dp[0]=0 and initialises dp[1] through dp[6] to infinity. For each x=1,2,...,6, scan coins in the fixed order 1,3,4. Whenever x-c>=0, relax dp[x] using 1+dp[x-c]. Every smaller amount is final before a larger amount reads it.
Both methods implement the same recurrence and must return the same answer. Memoization follows recursive dependencies and can avoid unreachable states. Tabulation removes call-stack overhead and exposes the legal computation order. A cache changes repeated work, not the mathematical definition. Neither method can repair a wrong state or transition.

Dynamic programming worked example: minimum coins for amount 6
Keep the coins unlimited and fixed at {1,3,4}. An invalid predecessor is impossible, so it must never be treated as a free zero-cost state.
| Candidate with coin | Candidate with coin | Candidate with coin |
|
|
|---|---|---|---|---|---|
| base case | base case | base case |
| none |
|
| invalid | invalid |
|
|
|
| invalid | invalid |
|
|
|
|
| invalid |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The fixed scan order resolves the tie at x=5 by recording coin 1, though coin 4 would give the same value. At x=6, the unique minimum candidate is 1+dp[3]=2, so pick[6]=3.
Each row reads only already final smaller amounts.
Reconstruct from 6: take coin 3 and move to 6-3=3; take coin 3 again and move to 3-3=0. The optimum is therefore two coins, {3,3}. A largest-coin-first shortcut chooses 4+1+1, which uses three coins. This counterexample shows why that greedy commitment is unsafe for these denominations.

Why the DP recurrence is correct and how much work it saves
Prove the recurrence by induction on amount x. The base x=0 needs zero coins. For x>0, every valid solution ends with some coin c, leaving x-c. If that remainder were not optimal, replacing it with a better solution would improve the original. Thus the minimum over all legal final coins is optimal for x.
For target A and m coin types, there are A+1 states. Each checks at most m coins, so both methods take O(mA) worst-case time and O(A) answer storage. With coin 1, top-down recursion can also use O(A) stack space. Uncached recursion revisits amounts along many paths.
Dynamic programming mistakes: what goes wrong and how to repair it
Mistake | What goes wrong | Repair |
|---|---|---|
Leaving | The formula can answer a different question | Define it as minimum coins for amount |
Using | The empty amount is charged | Use |
Treating a negative state as zero | An impossible predecessor looks free | Use infinity or skip that transition |
Confusing minimum coins with combinations |
| Use a counting state and addition |
Filling a state before its dependencies | A transition reads unfinished values | Process smaller amounts first |
Skipping the memoization cache check | Repeated states are recomputed | Return cached answers before branching |
This unbounded example permits reuse: 3+3. A 0/1 variant needs an item dimension or capacity order preventing reuse. “Take the largest coin” fails here, returning three coins instead of two.
How exams test an introduction to dynamic programming
Cue | What your answer must show |
|---|---|
Define the state | Explain what every index represents |
Derive the recurrence | Name the final choice and its predecessor |
Fill the table | Respect the dependency order |
Compare memoization and tabulation | Discuss evaluation order, stack use and storage |
Find complexity | Count states multiplied by work per state |
DP or greedy | Test whether a local commitment can fail |
Use the worked example for rapid checks. dp[4]=1. The chosen predecessor for dp[6] is dp[3] through coin 3. The final answer is 2, not 3. Overlapping subproblems explains why stored answers can be reused, while optimal substructure justifies building an optimum from smaller optimal answers.
Practise with about 20 Introduction to DP questions available in our practice bank. The GATE CS Exam Preparation page is the broader subject-navigation route.
Dynamic programming in the short version and the next step
Ask five questions: What does the state mean? Which choice creates the transition? What are the base and invalid states? In what order are dependencies available? Where is the final answer? Here, dp[0..6]=[0,1,2,1,1,2,2], and the optimum is 3+3.
Next, Dynamic Programming Explained: 0/1 Knapsack adds an item index and a take-or-skip choice. For a structured route through broader GATE CS preparation, GATE Guidance by Sanchit Sir is an optional next step.
Keep learning
Advanced Dynamic Programming: Mixed Problems, State Design and a GATE-Style Example
Learn how sorting, binary search and DP work together in weighted interval scheduling, then recover the chosen jobs and test the method on a changed input.
Dynamic Programming Interview Questions: 10 Worked Answers for Beginners
Build interview-ready DP reasoning through ten beginner questions, exact traces, language-neutral pseudocode and clear complexity analysis.
Introduction to Dynamic Programming MCQs: 12 Solved Questions with Explanations
Solve 12 introductory dynamic programming MCQs with clear explanations of core properties, memoization, Fibonacci, design steps, LIS, and table order.
Advanced Dynamic Programming MCQs: 12 Solved GATE and UGC NET Questions with Explanations
Solve 12 previous-year dynamic programming MCQs, then use the explanations to anchor the design paradigms, knapsack steps and complexity pairings they test.





