Asked in Publicis Sapient,Junior Engineer Associate
Permutations

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
CodingNinjas
author
2y

:

CodingNinjas
author
2y
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...read more
CodingNinjas
author
2y
Iterative 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...read more

CodingNinjas
author
2y
Heap’s Algorithm

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.
Space Complexity: OtherExplanation:

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

 

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

Time Complexity: O(n!)Explanation:

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

 

Since there are N! possible permutations. Thus the time complexity will be O(N!).

Add answer anonymously...
Publicis Sapient Junior Engineer Associate Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter