Skip to main content

All Unique Orderings

easy
BacktrackingRecursionArrayBacktracking Permutations
Asked atAmazonMicrosoftMeta

Problem Description

Given an array of **distinct** integers `sequence`, return **all possible permutations** of the array. The permutations may be returned in any order.

**Example 1:**
```
Input: sequence = [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: sequence = [7, 9]
Output: [[7,9],[9,7]]
```

Constraints

  • 1 <= sequence.length <= 8
  • All elements in sequence are distinct
  • -10 <= sequence[i] <= 10

Follow-up

Extend the solution to handle duplicate elements in `sequence`, returning only distinct permutations.

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.