Entringer Number in C++

C++Server Side ProgrammingProgramming

Entringer Number is a special number which is equal to the number of permutations of {1, 2, 3, … n+1}, starting with K+1 which is updated by decreasing then increasing the values alternatively.

The value of Entringer Number is formulated using,

The recurrence relation,

E(n, k) = E(n, k-1) + E(n-1, n-k)

The base value is,

E(0,0) = 1

E(n, 0) = 0

We can find the Entringer number using,

Let’s take an example to see values

N = 5, k = 3

E(5, 3) = 14

Program to illustrate the working of our solution,

Example

Live Demo

#include <iostream>
using namespace std;

int EntringerNumber(int n, int k)
{

   if (n == 0 && k == 0)
      return 1;
   if (k == 0)
      return 0;
   return EntringerNumber(n, k - 1) + EntringerNumber(n - 1, n - k);
}

int main() {

   int n = 5, k = 3;
   cout<<"The value of E("<<n<<", "<<k<<") = "<<EntringerNumber(n, k);
   return 0;
}

Output −

The value of E(5, 3) = 14
raja
Updated on 22-Jan-2021 12:49:29

Advertisements