Solvarch
DSA ProblemsProgressPlanRoadmapsReviewCheat SheetsCompaniesSystem DesignCS FundamentalsQuestion Bank
Theme
Problems

Permutations

Medium
backtrackingrecursion

Given an array of distinct integers, return all possible orderings (permutations).

Input: one line of space-separated integers. Output: one permutation per line, space-separated. Any generation order — the template prints canonically.

Example 1
Input:
1 2 3
Output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Example 2
Input:
1
Output:
1

Constraints

  • 1 ≤ nums.length ≤ 6
  • Distinct values.
Watch a video walkthrough

Run your code against the sample tests, or Submit to run the full suite.