Dsa
Backtracking
Medium
Rhythm Logic
A drummer is composing a pattern of length N. Each beat can be a Rest (0), a Snare (1), or a Kick (2). The pattern must follow these rules:
1. No more than two Rests (0) can occur consecutively.
2. A Kick (2) can never be followed immediately by another Kick.
3. The total number of Snares (1) in the entire pattern must be exactly K.
Read N and K from stdin. Output the total number of valid patterns.
Example:
Input: 3 1
Output: 9
(Valid: [0,1,0], [0,1,2], [1,0,0], [1,0,2], [1,2,0], [0,0,1], [2,1,0], [2,1,2], [2,0,1])
Key concepts
backtrackingsequence generationpruning
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