Count the number of elements which are greater than previous element
Given a range 1 to N. Among its different permutations you have to find out those permutations where only one element is greater than its previous element and you have to count that number.
For numbers 1 to 3, there are several permutations,
1 2 3 1 2 && 2 3
1 3 2 1 3
2 1 3 1 3
2 3 1 2 3
3 1 2 1 2
3 2 1 none
Input:
T no.of Testcases.
T no. Of lines along with the N values with it.
E.g.
3
2
3
4
Constrains:
1 ≤ T ≤ 10
1≤ N ≤ 9
Output:
Print the count of the permutations
those have only one element is greater than
the previous one.
Example
T=3
Input:
N=2
Output:
1 2
Count: 1
Input:
N=3
Output:
1 3 2
2 1 3
2 3 1
3 1 2
Count: 4
Input:
N=4
Output:
1 4 3 2
2 1 4 3
2 4 3 1
3 1 4 2
3 2 1 4
3 2 4 1
3 4 2 1
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
Count: 11
Let the range is N. Therefore, the numbers are X1, X2, X3, ..., XN.
Let f(i) = select an element in the permutation.
To know which elements are already visited in the array we will use a Boolean array and store the permutations we will use another array.
To find out the permutations we will follow these following steps,
C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer