Dsa
Backtracking
Hard
Garden Path Arrangement
You are given a garden path represented by an array of flower types, where each type is denoted by a letter (like 'R' for Rose, 'T' for Tulip, etc.). Your task is to arrange the flowers such that no two adjacent flowers are of the same type. Print all possible valid arrangements.
Input Format:
The first line contains an integer n (1 ≤ n ≤ 10), the number of flowers.
The second line contains n space-separated characters representing flower types (e.g., R T R T).
Output Format:
Print each valid arrangement on a new line in lexicographical order.
Example:
Input:
4
R T R T
Output:
R T R T
R T T R
T R R T
T R T R
Key concepts
backtrackingcombinatoricspathfinding
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