Permutations - Problem

Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order.

A permutation is a rearrangement of all elements in the array where each element appears exactly once in a different order.

Input & Output

Example 1 — Basic Three Elements
$ Input: nums = [1,2,3]
Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
💡 Note: All 6 possible arrangements of [1,2,3]: start with each element, then arrange the remaining two elements in all possible ways
Example 2 — Two Elements
$ Input: nums = [0,1]
Output: [[0,1],[1,0]]
💡 Note: Only 2 possible arrangements: [0,1] and [1,0]. With 2 elements, we have 2! = 2 permutations
Example 3 — Single Element
$ Input: nums = [1]
Output: [[1]]
💡 Note: With only one element, there's only one possible arrangement: [1]. 1! = 1 permutation

Constraints

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

Visualization

Tap to expand
Permutations Problem: Find All Arrangements123Input: [1, 2, 3]Generate All Arrangements ⟶[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2][3,2,1]Output: 6 permutations (3! = 3×2×1)
Understanding the Visualization
1
Input
Array of distinct integers [1,2,3]
2
Process
Generate all possible arrangements using backtracking
3
Output
All 6 permutations in 2D array format
Key Takeaway
🎯 Key Insight: Use backtracking to systematically explore all possible arrangements by making choices and undoing them
Asked in
Amazon 45 Microsoft 38 Google 32 Apple 28
464.0K Views
High Frequency
~15 min Avg. Time
14.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen