Last Updated: 18 Nov, 2020

Permutations

Moderate
Asked in companies
CIS - Cyber InfrastructureTata Consultancy Services (TCS)Flipkart

Problem statement

A permutation is a mathematical technique that determines the number of possible arrangements in a set when the order of the arrangements matters. A string of length 'N' has 'N'! permutations.

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

Example:
'ARR[]' = [1, 2]

The size of the array is 2. So, the total number of permutations is 2! = 2. The possible permutations are [1, 2] (the array itself) and [2,1] where the position of element 1 in the original array is swapped with element 2 and vice-versa.   
Note:
1. All the numbers in the array are unique.

2. You can return the answer in any order.

3. The original array is also a permutation of the given array.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.

The first line of each test case contains ‘N’ denoting the total number of elements in the array.

The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array 'ARR' whose all possible permutations are to be calculated.
Output Format:
For each test case, return all the possible permutations of the given array of integers.
Note:
You don't need to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 10
1 <= N <= 7
-10 ^ 9 <= ARR[i] <= 10 ^ 9

Where ‘ARR[i]’ denotes the range of elements in the array.

Time limit: 1 sec

Approaches

01 Approach

The steps are as follows:

 

  1. Find the size of the given vector and store it in a variable ‘N’.
  2. Make a function call to ‘permute’ and send the vector, ‘i’=0 and ‘r’ = ‘N’ - 1 along.
  3. In the function ‘permute’, if ‘l’ is equal to ‘r’, that is, the last element is encountered, add the updated values in the ‘ANSWER’ vector as one of the possible permutations, and return the function This is the base case for the recursion.
  4. Run a loop where ‘i’ ranges from ‘l’ to ‘r’:
        a. Swap ‘VEC[l]’ and ‘VEC[i]’.
        b. Recur with ‘l’ updated to ‘l' + 1.
        c. Backtrack by swapping ‘VEC[l]’ and ‘VEC[i]’. In the above figure, at level 0 we have [1, 2, 3]. After swapping 1 with 2, the original vector changes to [2, 1, 3]. After generating the permutations using this vector, we need to backtrack to get the original vector [1, 2, 3] and this is why we swap again after recursion. This occurs after every recursion to get the original set of numbers.

5.  Return the result vector.

02 Approach

The idea is to sort the vector and repeatedly generate the next greater lexicographic permutation of a vector iteratively, in order to print all permutations of the vector.

 

The steps are as follows:

 

  1. Sort the given vector in ascending order.
  2. In a while loop, initialize variable ‘i’ to the last index of the given vector.
  3. Store the earlier permutation obtained in ‘ANSWER’ vector.
  4. While ‘VEC[i-1]’ is greater than or equal to ‘VEC[i]’, decrement ‘i’. If ‘i’ reaches the 0th index of the given vector, we are already at the last permutation and hence we return to the main function as all permutations have been stored in the vector ‘ANSWER’.
  5. Initialize variable ‘j’ to the last index of the given vector.
  6. While ‘j’ is greater than ‘i’ and ‘VEC[j]’ is lesser than or equal to ‘VEC[i - 1]’, decrement ‘j’.
  7. Swap ‘VEC[i - 1]’ and ‘VEC[j]’.
  8. Reverse the subarray ‘VEC['i'...'SIZE' - 1]’.

03 Approach

Learn about the Heap’s algorithm in detail from the below link:

https://medium.com/sodalabs/heaps-algorithm-fun-observation-4986a188a80

 

Heap’s algorithm fixes the element in the last position and constructs all permutations for the rest of the elements in place. After that, the algorithm swaps the element in the last position with one of the rest and repeats the process. This is obtained in the following way:

  1. If ‘N’ is odd, swap elements at position 1 and ‘N’.
  2. If ‘N’ is even, swap elements in position ‘i’ and ‘N’.

 

The steps are as follows:

 

  • Find the size of the given vector and store it in a variable ‘N’.
  • Make a function call to ‘permute’ and send the vector, size ‘N’ along.
  • In the function ‘PERMUTE’
         a. Run a loop where ‘i’ ranges from ‘0’ to ‘N' - 1. The last element is fixed and permutations for ‘N’ - 2 elements are found by recursion and the loop.
        b. If ‘N’ is odd, swap the first and last element and if ‘N' is even, then swap the ith element (‘i’ is the counter starting from 0) and the last element and repeat the above algorithm till ‘i’ is less than ‘N’.
        c. In each iteration, the algorithm will produce all the permutations that end with the current last element. Store the permutation in ‘ANSWER’ vector obtained after every iteration.