![]() |
VOOZH | about |
The Entringer Number E(n, k) are the number of permutations of {1, 2, ..., n + 1}, starting with k + 1, which, after initially falling, alternatively fall then rise. The Entringer are given by:
For example, for n = 4 and k = 2, E(4, 2) is 4.
They are:
3 2 4 1 5
3 2 5 1 4
3 1 4 2 5
3 1 5 2 4
Examples :
Input : n = 4, k = 2
Output : 4
Input : n = 4, k = 3
Output : 5
Below is program to find Entringer Number E(n, k). The program is based on above simple recursive formula.
5
Below is the implementation of finding Entringer Number using Dynamic Programming:
5
Time Complexity: O(n * n)
Auxiliary Space: O(n * k)
Efficient approach : Space optimization
In previous approach the current value dp[i][j] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 1D array to store the computations.
Implementation steps:
Implementation:
5
Time complexity: O(N*K)
Auxiliary Space: O(K)