Bootcamps
Masterclasses
Menu
Courses
Account
Login / Sign Up

Permutation of an Array

Medium Acceptance 68% Points 30

You are given an array of integers. Your task is to generate all possible permutations of the array. A permutation is formed by rearranging the elements in every possible order. Return all permutations of the array.

Examples
Example 1

Example 1

Input: arr[] = [5]

Output: [5]

Explanation: Only one arrangement is possible.

Example 2

Example 2

Input: arr[] = [1 2]

Output: [1 2] [2 1]

Explanation: The two elements can swap positions to form different orders.

Example 3

Example 3

Input: arr[] = [1 2 3]

Output: [1 2 3] [1 3 2] [2 1 3] [2 3 1] [3 1 2] [3 2 1]

Explanation: All possible rearrangements of the three elements are generated.

Constraints
  • 1 = n = 8
  • -10? = arr[i] = 10?
Topics
Array Recursion
Companies
Amazon Microsoft Apple Samsung Visa Walmart Adobe Uber Paypal Intuit
Added 19 Jul 2026 68% acceptance
Ln 1, Col 1Sign in to save your code
1 2 3
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1