ExamAdda Logo

Permutations

Easy

Given an array nums of distinct integers, return all possible permutations of the elements.

A permutation is an arrangement of all the elements of the array in a different order.

Return the permutations in any order.

Example 1

Input

n = 3
nums = [1, 2, 3]

Output

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Explanation

There are 3! = 6 possible arrangements of [1,2,3].

Example 2

Input

n = 2
nums = [0, 1]

Output

0 1
1 0

Explanation

The two possible permutations are [0,1] and [1,0].

Constraints

1 <= n <= 6
nums.length == n
-10 <= nums[i] <= 10
All elements in nums are distinct.
There are exactly n! possible permutations.

Hints:

Hint 1

Build the permutation one element at a time.

Hint 2

Keep track of which elements have already been used in the current permutation.

Author & Technical Reviewer

ExamAdda Technical Review Team

Technically reviewed by: ExamAdda Technical Review Team

Technical Reviewers, ExamAdda

Software engineers at ExamAdda who check every article's definitions, complexity claims and code examples before and after publishing.

Published

Aug 20, 2026

Last updated

Aug 21, 2026

Content Verification Methodology

Definitions and complexity claims were checked against authoritative computer-science references. Code examples were compiled and tested with standard, boundary and edge-case inputs.

Report a correction:editorial@examadda.org
Auto
Loading editor...
Input

Expected Output