VOOZH about

URL: https://www.geeksforgeeks.org/dsa/entringer-number/

⇱ Entringer Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Entringer Number

Last Updated : 15 May, 2024

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: 

👁 Image


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. 


Output
5

Below is the implementation of finding Entringer Number using Dynamic Programming: 


Output
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:

  • Create a 1D vector dp of size K+1.
  • Set a base case by initializing the values of DP .
  • Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
  • Now Create a temporary 1d vector curr used to store the current values from previous computations.
  • After every iteration assign the value of curr to dp for further iteration.
  • At last return and print the final answer stored in dp[k].

Implementation:


Output
5

Time complexity: O(N*K)
Auxiliary Space: O(K)

Comment
Article Tags: