Permutations
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
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
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.
Expected Output