Dsa
Dynamic Programming
Warm-up
Painting Fence
A fence with `n` sections needs to be painted. Each section can be painted in one of `k` colors. However, two adjacent sections cannot be painted the same color. Find the number of ways to paint the fence.
Input format:
- Two integers, `n` (sections) and `k` (colors) (1 <= n <= 50, 1 <= k <= 10).
Output format:
- A single integer, the number of ways to paint the fence.
Example:
Input:
3 2
Output:
6
Key concepts
dynamic_programmingcombinatorialcost_minimization
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now