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.
C++
#include <bits/stdc++.h>
using namespace std;
int zigzag( int n, int k)
{
if (n == 0 && k == 0)
return 1;
if (k == 0)
return 0;
return zigzag(n, k - 1) +
zigzag(n - 1, n - k);
}
int main()
{
int n = 4, k = 3;
cout << zigzag(n, k) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int zigzag( int n, int k)
{
if (n == 0 && k == 0 )
return 1 ;
if (k == 0 )
return 0 ;
return zigzag(n, k - 1 ) +
zigzag(n - 1 , n - k);
}
public static void main(String[] args)
{
int n = 4 , k = 3 ;
System.out.println(zigzag(n, k));
}
}
|
Python3
def zigzag(n, k):
if (n = = 0 and k = = 0 ):
return 1
if (k = = 0 ):
return 0
return zigzag(n, k - 1 ) + zigzag(n - 1 , n - k);
n = 4
k = 3
print (zigzag(n, k))
|
C#
using System;
class GFG {
static int zigzag( int n, int k)
{
if (n == 0 && k == 0)
return 1;
if (k == 0)
return 0;
return zigzag(n, k - 1) +
zigzag(n - 1, n - k);
}
public static void Main()
{
int n = 4, k = 3;
Console.WriteLine(zigzag(n, k));
}
}
|
PHP
<?php
function zigzag( $n , $k )
{
if ( $n == 0 and $k == 0)
return 1;
if ( $k == 0)
return 0;
return zigzag( $n , $k - 1) +
zigzag( $n - 1, $n - $k );
}
$n = 4; $k = 3;
echo zigzag( $n , $k ) ;
?>
|
Javascript
<script>
function zigzag( n, k)
{
if (n == 0 && k == 0)
return 1;
if (k == 0)
return 0;
return zigzag(n, k - 1) +
zigzag(n - 1, n - k);
}
n = 4;
k = 3;
document.write( zigzag(n, k));
</script>
|
Below is the implementation of finding Entringer Number using Dynamic Programming:
C++
#include <bits/stdc++.h>
using namespace std;
int zigzag( int n, int k)
{
int dp[n + 1][k + 1];
memset (dp, 0, sizeof (dp));
dp[0][0] = 1;
for ( int i = 1; i <= n; i++)
dp[i][0] = 0;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= i; j++)
dp[i][j] = dp[i][j - 1] + dp[i - 1][i - j];
}
return dp[n][k];
}
int main()
{
int n = 4, k = 3;
cout << zigzag(n, k) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int zigzag( int n, int k)
{
int dp[][] = new int [n + 1 ][k + 1 ];
dp[ 0 ][ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++)
dp[i][ 0 ] = 0 ;
for ( int i = 1 ; i <= n; i++) {
for ( int j = 1 ; j <= Math.min(i, k);
j++)
dp[i][j] = dp[i][j - 1 ] +
dp[i - 1 ][i - j];
}
return dp[n][k];
}
public static void main(String[] args)
{
int n = 4 , k = 3 ;
System.out.println(zigzag(n, k));
}
}
|
Python3
def zigzag(n, k):
dp = [[ 0 for x in range (k + 1 )]
for y in range (n + 1 )]
dp[ 0 ][ 0 ] = 1
for i in range ( 1 , n + 1 ):
dp[i][ 0 ] = 0
for i in range ( 1 , n + 1 ):
for j in range ( 1 , k + 1 ):
dp[i][j] = (dp[i][j - 1 ]
+ dp[i - 1 ][i - j])
return dp[n][k]
n = 4
k = 3
print (zigzag(n, k))
|
C#
using System;
class GFG {
static int zigzag( int n, int k)
{
int [, ] dp = new int [n + 1, k + 1];
dp[0, 0] = 1;
for ( int i = 1; i <= n; i++)
dp[i, 0] = 0;
for ( int i = 1; i <= n; i++) {
for ( int j = 1; j <= Math.Min(i, k);
j++)
dp[i, j] = dp[i, j - 1] + dp[i - 1, i - j];
}
return dp[n, k];
}
public static void Main()
{
int n = 4, k = 3;
Console.WriteLine(zigzag(n, k));
}
}
|
PHP
<?php
function zigzag( $n , $k )
{
$dp = array ( array ());
$dp [0][0] = 1;
for ( $i = 1; $i <= $n ; $i ++)
$dp [ $i ][0] = 0;
for ( $i = 1; $i <= $n ; $i ++)
{
for ( $j = 1; $j <= $i ; $j ++)
$dp [ $i ][ $j ] = $dp [ $i ][ $j - 1] +
$dp [ $i - 1][ $i - $j ];
}
return $dp [ $n ][ $k ];
}
$n = 4; $k = 3;
echo zigzag( $n , $k );
?>
|
Javascript
<script>
function zigzag(n, k)
{
let dp = new Array(n+1);
for ( var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
dp[0][0] = 1;
for (let i = 1; i <= n; i++)
dp[i][0] = 0;
for (let i = 1; i <= n; i++) {
for (let j = 1; j <= Math.min(i, k);
j++)
dp[i][j] = dp[i][j - 1] +
dp[i - 1][i - j];
}
return dp[n][k];
}
let n = 4, k = 3;
document.write(zigzag(n, k));
</script>
|
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:
C++
#include <bits/stdc++.h>
using namespace std;
int zigzag( int n, int k)
{
vector< int >dp(k+1,0);
dp[0] = 1;
for ( int i = 1; i <= n; i++) {
vector< int >curr(k+1,0);
for ( int j = 1; j <= i; j++){
curr[j] = curr[j - 1] + dp[i - j];
}
dp = curr;
}
return dp[k];
}
int main()
{
int n = 4, k = 3;
cout << zigzag(n, k) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int zigzag( int n, int k) {
int [] dp = new int [n+ 1 ];
Arrays.fill(dp, 0 );
dp[ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++) {
int [] curr = new int [n+ 1 ];
Arrays.fill(curr, 0 );
for ( int j = 1 ; j <= i; j++){
curr[j] = curr[j - 1 ] + dp[i - j];
}
dp = curr;
}
return dp[k];
}
public static void main(String[] args) {
int n = 4 , k = 3 ;
System.out.println(zigzag(n, k));
}
}
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program {
public static int zigzag( int n, int k)
{
int [] dp = new int [n + 1];
Array.Fill(dp, 0);
dp[0] = 1;
for ( int i = 1; i <= n; i++) {
int [] curr = new int [n + 1];
Array.Fill(curr, 0);
for ( int j = 1; j <= i; j++) {
curr[j] = curr[j - 1] + dp[i - j];
}
dp = curr;
}
return dp[k];
}
static void Main( string [] args)
{
int n = 4, k = 3;
Console.WriteLine(zigzag(n, k));
}
}
|
Javascript
function zigzag(n, k) {
let dp = new Array(k + 1).fill(0);
dp[0] = 1;
for (let i = 1; i <= n; i++) {
let curr = new Array(k + 1).fill(0);
for (let j = 1; j <= i; j++) {
curr[j] = curr[j - 1] + dp[i - j];
}
dp = curr;
}
return dp[k];
}
function main() {
let n = 4;
let k = 3;
let ans = zigzag(n, k);
console.log(ans);
}
main();
|
Time complexity: O(N*K)
Auxiliary Space: O(K)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!