Skip to content
AI360Xpert
Beta
Difficulty: MediumBacktracking

Permutations

Problem in Plain English

Given distinct integers, return every possible ordering that uses each element exactly once. All orderings are collected and their sequence does not matter.

Problem Statement

Given an array nums of distinct integers, return every possible ordering of all its elements.

Each ordering must use every element exactly once, so an ordering is a rearrangement of the full array, not a shorter selection.

Return the full list in any order.

Constraints

  • 1 <= nums.length <= 6
  • -10 <= nums[i] <= 10
  • All the integers of nums are unique.

Examples

Example 1

Input
nums = [1,2,3]
Output
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

There are three choices for the first slot, two for the next, and one for the last: six orderings. Each contains 1, 2, and 3 once. Changing their positions creates a distinct answer.

Example 2

Input
nums = [0,1]
Output
[[0,1],[1,0]]

Zero is an ordinary value, not an unused marker. Either 0 or 1 comes first and the other must follow, giving exactly two answers.

Example 3

Input
nums = [1]
Output
[[1]]

The only slot must contain 1. The result still has an outer list of answers and an inner one-element ordering.

Intuition

An ordering chooses who occupies each slot, so [1,2,3] and [1,3,2] are different answers. The challenge is using every input exactly once. A baseline fills slots freely and rejects repeats at the end; swapping instead keeps used values in a fixed prefix and remaining values in a suffix. Backtracking restores each swap before trying another candidate, so one branch cannot corrupt the next.

Approaches

Unguided Guessing

Solution Details

Reveal Unguided Guessing: intuition, complexity, and code

Hints

Hint 1
Each answer uses every element exactly once, so every recursion level can fix one more position.
Hint 2
Track which elements are already placed, either with a used array or by swapping placed elements to the front.
Hint 3
Swap a candidate into the current slot, recurse to the next slot, then swap back so sibling branches start clean.

Edge Cases

  • A single element [1] has exactly one ordering, [[1]], with no swaps needed.
  • Two elements [0, 1] give [[0, 1], [1, 0]], the smallest case that actually branches.
  • Negative numbers and zero permute exactly like positives because only positions matter, never values.

Common Mistakes and Interview Tips

  • Forgetting to swap back after recursing, which hands the next sibling a scrambled array and fabricates orderings.
  • Recording the live array instead of a copy, so every stored answer ends up identical to the final state.
  • Stopping the loop at the current slot instead of scanning the whole tail, which strands unplaced elements and loses orderings.
  • Calling the swap approach constant-space without counting its recursion stack or the n! copied output rows.

Key Takeaway

Ordering problems are slot-filling walks: fix one position per level from the still-unused pool and undo the choice on the way back up.