Eulerian Number in C++


In mathematics, the Eulerian number is a special type of combination number. It defines the number of permutations in which the next element is a specific number greater than the previous one.

Denoted as, 

A(n, m) is permutation from 1 to n in which two numbers vary by m.

Problem Statement: In this problem, we are given two numbers m and n. And we need to find the number of permutations that are the Eulerian Number.

Let’s take an example to understand the problem,

Input:  n = 4, m = 2

Output: 11

Explanation: 

All permutations of number from 1 to 4 are −

1 2 3 4         1 2 4 3         1 3 2 4         1 3 4 2         1 4 2 3         1 4 3 2
2 1 3 4         2 1 4 3         2 3 1 4         2 3 4 1         2 4 1 3         2 4 3 1
3 1 2 4         3 1 4 2         3 2 1 4         3 2 4 1         3 4 1 2         3 4 2 1
4 1 2 3         4 1 3 2         4 2 1 3         4 2 3 1         4 3 1 2         4 3 2 1

Out of all 11 permutations have the difference between two numbers m. 


Solution Approach −

To find the number of permutations, we will be using the formula for eulerian number,

A(n, m) = 0, if m > n or n = 0
           A(n, m) = 1, if m = 0
           A(n, m) = (n-m)A(n-1, m-1) + (m+1)A(n-1, m)
 


Program to illustrate the working of our solution,

Example

Live Demo


#include <iostream>
using namespace std;

int countEulerianNumber(int n, int m)
{
   if (m >= n || n == 0)
      return 0;
   if (m == 0)
      return 1;
   return ( ( (n - m) * countEulerianNumber(n - 1, m - 1) ) + ( (m + 1) * countEulerianNumber(n - 1, m) ) );
}

int main() {

   int n = 5, m = 3;
   cout<<"The number of Eulerian permutations is "<<countEulerianNumber(n, m);
   return 0;
}

Output −

The number of Eulerian permutations is 26

Updated on: 22-Jan-2021

173 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements