All Permutation of an Array with Distinct Elements (original) (raw)
Last Updated : 24 Sep, 2025
Given an array **arr[], generate all possible permutations of the elements in the array.
**Note: All elements in the array are distinct.
**Examples:
**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: There are 6 possible permutations (3! =6) of the array.**Input: arr[] = [0]
**Output: [[0]]
[Approach] Using Recursion and Backtracking - O(n!) Time and O(n) Space
The idea is to fix one element at a time and recursively generate permutations for the rest. At each step, we swap the current element with another, explore further recursively, and then backtrack by swapping back.
We simply start at the first index, swap it with each possible choice, and recurse for the next index. After each recursive call, we backtrack by swapping back, so the array is restored for the next possibility. Repeating this until the end ensures every permutation is covered exactly once.

Pseudo-code Idea:
**Base case: If idx == n, store the current arrangement as one valid permutation.
**Recursive choices: For every index idx, we fix one element by swapping it with each element from idx to the last index.
- Swap arr[idx] with arr[i].
- Recurse with (idx + 1) to the remaining array.
- Backtrack by swapping arr[idx] and arr[i] again to restore the original state. C++ ``
#include #include using namespace std;
// Recursive function to find all possible permutations. void permutations(vector<vector>& res, vector arr, int idx) { if (idx == arr.size()) { res.push_back(arr); return; }
// Permutations made by swapping each element
// starting from index `idx`
for (int i = idx; i < arr.size(); i++) {
// Swapping
swap(arr[idx], arr[i]);
// Recursive call to create permutations
// for the next element
permutations(res, arr, idx + 1);
// Backtracking
swap(arr[idx], arr[i]);
}}
// Function to get the permutations vector<vector> permuteDist(vector& arr) { vector<vector> res; permutations(res, arr, 0); return res; }
int main() { vector arr = {1, 2, 3}; vector<vector> res = permuteDist(arr); for (auto x : res) { for (auto y : x) { cout << y << " "; } cout << endl; }
return 0;}
`` Java `
import java.util.ArrayList;
public class Main { // Recursive function to find all possible permutations static void permutations(ArrayList<ArrayList> res, int[] arr, int idx) { if (idx == arr.length) { ArrayList temp = new ArrayList<>(); for (int val : arr) temp.add(val); res.add(temp); return; }
for (int i = idx; i < arr.length; i++) {
// Swapping
int temp = arr[idx];
arr[idx] = arr[i];
arr[i] = temp;
// Recursive call
permutations(res, arr, idx + 1);
// Backtracking
temp = arr[idx];
arr[idx] = arr[i];
arr[i] = temp;
}
}
// Function to get the permutations
static ArrayList<ArrayList<Integer>> permuteDist(int[] arr) {
ArrayList<ArrayList<Integer>> res = new ArrayList<>();
permutations(res, arr, 0);
return res;
}
public static void main(String[] args) {
int[] arr = {1, 2, 3};
ArrayList<ArrayList<Integer>> res = permuteDist(arr);
for (ArrayList<Integer> x : res) {
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
}
}}
` Python ``
Recursive function to find all possible permutations
def permutations(res, arr, idx): if idx == len(arr): res.append(arr[:]) return
# Permutations made by swapping each element starting from index `idx`
for i in range(idx, len(arr)):
# Swapping
arr[idx], arr[i] = arr[i], arr[idx]
# Recursive call
permutations(res, arr, idx + 1)
# Backtracking
arr[idx], arr[i] = arr[i], arr[idx]Function to get the permutations
def permuteDist(arr): res = [] permutations(res, arr, 0) return res
if name == "main": arr = [1, 2, 3] res = permuteDist(arr) for x in res: for y in x: print(y, end=" ") print()
`` C# `
using System; using System.Collections.Generic;
class Program { // Recursive function to find all possible permutations static void permutations(List<List> res, int[] arr, int idx) { if (idx == arr.Length) { res.Add(new List(arr)); return; }
for (int i = idx; i < arr.Length; i++) {
// Swapping
int temp = arr[idx];
arr[idx] = arr[i];
arr[i] = temp;
// Recursive call
permutations(res, arr, idx + 1);
// Backtracking
temp = arr[idx];
arr[idx] = arr[i];
arr[i] = temp;
}
}
// Function to get the permutations
static List<List<int>> permuteDist(int[] arr) {
List<List<int>> res = new List<List<int>>();
permutations(res, arr, 0);
return res;
}
static void Main() {
int[] arr = { 1, 2, 3 };
List<List<int>> res = permuteDist(arr);
foreach (var x in res) {
foreach (var y in x) {
Console.Write(y + " ");
}
Console.WriteLine();
}
}}
` JavaScript ``
// Recursive function to find all possible permutations function permutations(res, arr, idx) { if (idx === arr.length) { res.push([...arr]); return; }
// Permutations made by swapping each element starting from index `idx`
for (let i = idx; i < arr.length; i++) {
// Swapping
[arr[idx], arr[i]] = [arr[i], arr[idx]];
// Recursive call
permutations(res, arr, idx + 1);
// Backtracking
[arr[idx], arr[i]] = [arr[i], arr[idx]];
}}
// Function to get the permutations function permuteDist(arr) { let res = []; permutations(res, arr, 0); return res; }
// Driver code let arr = [1, 2, 3]; let res = permuteDist(arr); for (let x of res) { console.log(x.join(" ")); }
``
Output
1 2 3 1 3 2 2 1 3 2 3 1 3 2 1 3 1 2