Permutations

Moderate
0/80
Average time to solve is 10m
profile
Contributed by
26 upvotes
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.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample input 1:
2
3
1 2 3 
1
1
Sample Output 1:
1 2 3   1 3 2   2 1 3  2 3 1  3 1 2   3 2 1
1
Explanation of Sample Output 1:
In test case 1, For [1,2,3], size of the array is 3. Therefore, number of permutations is 3!= 6. The possible 6 permutations are [[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]].

In test case 2, For [1], the size of the array is 1. Therefore, the number of permutations is 1!= 1. The only possible permutation is [1].
Sample input 2:
2
2
0 1
3
4 5 6
Sample output 2:
0 1  1 0
4 5 6   4 6 5   5 4 6   5 6 4   6 4 5   6 5 4
Explanation of Sample Output 2:
In test case 1, For [0, 1], size of the array is 2. Therefore, number of permutations is 2! = 2. The possible 2 permutations are [[0, 1], [1, 0]].

In test case 2, For [4, 5, 6], the size of the array is 3. Therefore, the number of permutations is 3! =  6. The possible 6 permutations are [[4, 5, 6], [4, 6, 5], [5, 4, 6], [5, 6, 4], [6, 4, 5], [6, 5, 4]].
Hint

Will swapping elements and backtracking help?

Approaches (3)
Using backtracking

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.

Time Complexity

O(N * N!), Where ‘N’ is the size of the vector.

 

Since there are N! permutations and it requires O(N) time to print a permutation. Thus the time complexity will be O(N * N!).

Space Complexity

O(N * N!), Where ‘N’ is the size of the vector.

 

Since the recursive stack uses space of order ‘N’. Also, the ‘ANSWER’ vector of size (N * N!) is used to store all possible permutations. Thus the space complexity will be O(N * N!).

Code Solution
(100% EXP penalty)
Permutations
Full screen
Console