Print all possible permutations of an Array/Vector without duplicates using Backtracking - GeeksforGeeks

08 Dec, 2021
Given vector nums, the task is to print all the possible permutations of the given vector using backtracking 
Examples:
Input: nums[] = {1, 2, 3}Output: {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 2, 1}, {3, 1, 2}Explanation: There are 6 possible permutations
Input: nums[] = {1, 3}Output: {1, 3}, {3, 1}Explanation: There are 2 possible permutations
Approach: The task can be solved with the help of backtracking. A similar article for better understanding is here: Print all permutations of a given string 
Below is the implementation of the above code:
C++
Python3
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function for swapping two numbersvoid swap(int& x, int& y){    int temp = x;    x = y;    y = temp;} // Function to find the possible// permutationsvoid permutations(vector<vector<int> >& res,                  vector<int> nums, int l, int h){    // Base case    // Add the vector to result and return    if (l == h) {        res.push_back(nums);        return;    }     // Permutations made    for (int i = l; i <= h; i++) {         // Swapping        swap(nums[l], nums[i]);         // Calling permutations for        // next greater value of l        permutations(res, nums, l + 1, h);         // Backtracking        swap(nums[l], nums[i]);    }} // Function to get the permutationsvector<vector<int> > permute(vector<int>& nums){    // Declaring result variable    vector<vector<int> > res;    int x = nums.size() - 1;     // Calling permutations for the first    // time by passing l    // as 0 and h = nums.size()-1    permutations(res, nums, 0, x);    return res;} // Driver Codeint main(){    vector<int> nums = { 1, 2, 3 };    vector<vector<int> > res = permute(nums);     // printing result    for (auto x : res) {        for (auto y : x) {            cout << y << " ";        }        cout << endl;    }     return 0;}
# Python program for the above approach # Function to find the possible# permutationsdef permutations(res, nums, l, h) :         # Base case    # Add the vector to result and return    if (l == h) :        res.append(nums);        for i in range(len(nums)):            print(nums[i], end=' ');         print('')        return;     # Permutations made    for i in range(l, h + 1):                 # Swapping        temp = nums[l];        nums[l] = nums[i];        nums[i] = temp;         # Calling permutations for        # next greater value of l        permutations(res, nums, l + 1, h);         # Backtracking        temp = nums[l];        nums[l] = nums[i];        nums[i] = temp; # Function to get the permutationsdef permute(nums):         # Declaring result variable    x = len(nums) - 1;    res = [];         # Calling permutations for the first    # time by passing l    # as 0 and h = nums.size()-1    permutations(res, nums, 0, x);    return res; # Driver Codenums = [ 1, 2, 3 ];res = permute(nums); # This code is contributed by Saurabh Jaiswal
<script> // JavaScript program for the above approach // Function to find the possible// permutationsfunction permutations(res, nums, l, h){         // Base case    // Add the vector to result and return    if (l == h)    {        res.push(nums);        for(let i = 0; i < nums.length; i++)            document.write(nums[i] + ' ');         document.write('<br>')        return;    }     // Permutations made    for(let i = l; i <= h; i++)    {                 // Swapping        let temp = nums[l];        nums[l] = nums[i];        nums[i] = temp;         // Calling permutations for        // next greater value of l        permutations(res, nums, l + 1, h);         // Backtracking        temp = nums[l];        nums[l] = nums[i];        nums[i] = temp;    }} // Function to get the permutationsfunction permute(nums){         // Declaring result variable    let x = nums.length - 1;    let res = [];         // Calling permutations for the first    // time by passing l    // as 0 and h = nums.size()-1    permutations(res, nums, 0, x);    return res;} // Driver Codelet nums = [ 1, 2, 3 ];let res = permute(nums); // This code is contributed by Potta Lokesh </script>
1 2 3 
1 3 2 
2 1 3 
2 3 1 
3 2 1 
3 1 2 
Time Complexity: O(N*N!) Note that there are N! permutations and it requires O(N) time to print a permutation.Auxiliary Space: O(r – l)
lokeshpotta20
_saurabh_jaiswal
permutation
Arrays
Backtracking
Combinatorial
Recursion
Arrays
Recursion
permutation
Combinatorial
Backtracking
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Introduction to Arrays
Multidimensional Arrays in Java
Linear Search
Linked List vs Array
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
N Queen Problem | Backtracking-3
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Backtracking | Introduction
Rat in a Maze | Backtracking-2
The Knight's tour problem | Backtracking-1