Dsa
Backtracking
Medium
Permutation Paths in Grid
Given an n x n grid filled with unique integers from 1 to n^2, you need to find all unique permutations of numbers starting from the top-left corner (1,1) to the bottom-right corner (n,n). A valid move can either be to the right or down. Print each permutation on a new line, in lexicographical order. Each permutation should be formatted as a space-separated string of integers.
Example Input:
2
1 2
3 4
Example Output:
1 2 4 3
1 3 2 4
2 1 3 4
2 1 4 3
2 3 1 4
2 4 1 3
3 1 2 4
3 1 4 2
3 2 1 4
3 4 1 2
4 1 2 3
4 1 3 2
4 2 1 3
4 3 1 2
Key concepts
backtrackinggridpathfinding
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