In combinatorics, the Eulerian Number A(n, m), is the number of permutations of the numbers 1 to n in which exactly m elements are greater than previous element.
For example, there are 4 permutations of the number 1 to 3 in which exactly 1 element is greater than the previous elements.
Examples:
Input : n = 3, m = 1 Output : 4 Please see above diagram (There are 4 permutations where 1 no. is greater.
Input : n = 4, m = 1 Output : 11
Eulerian Numbers are the coefficients of the Eulerian polynomials described below.
The Eulerian polynomials are defined by the exponential generating function
The Eulerian polynomials can be computed by the recurrence
An explicit formula for A(n, m) is
We can calculate A(n, m) by recurrence relation:
Example:
Suppose, n = 3 and m = 1. Therefore, A(3, 1) = (3 - 1) * A(2, 0) + (1 + 1) * A(2, 1) = 2 * A(2, 0) + 2 * A(2, 1) = 2 * 1 + 2 * ( (2 - 1) * A(1, 0) + (1 + 1) * A(1, 1)) = 2 + 2 * (1 * 1 + 2 * ((1 - 1) * A(0, 0) + (1 + 1) * A(0, 1)) = 2 + 2 * (1 + 2 * (0 * 1 + 2 * 0) = 2 + 2 * (1 + 2 * 0) = 2 + 2 * 1 = 2 + 2 = 4 We can verify this with example shown above.
Below is the implementation of finding A(n, m):
C++
// CPP Program to find Eulerian number A(n, m) #include <bits/stdc++.h> using namespace std; // Return euleriannumber A(n, m) int eulerian( int n, int m) { if (m >= n || n == 0) return 0; if (m == 0) return 1; return (n - m) * eulerian(n - 1, m - 1) + (m + 1) * eulerian(n - 1, m); } // Driven Program int main() { int n = 3, m = 1; cout << eulerian(n, m) << endl; return 0; } |
Java
// Java program to find Eulerian number A(n, m) import java.util.*; class Eulerian { // Return eulerian number A(n, m) public static int eulerian( int n, int m) { if (m >= n || n == 0 ) return 0 ; if (m == 0 ) return 1 ; return (n - m) * eulerian(n - 1 , m - 1 ) + (m + 1 ) * eulerian(n - 1 , m); } // driver code public static void main(String[] args) { int n = 3 , m = 1 ; System.out.print(eulerian(n, m)); } } // This code is contributed by rishabh_jain |
Python3
# Python3 Program to find Eulerian number A(n, m) # Return euleriannumber A(n, m) def eulerian(n, m): if (m > = n or n = = 0 ): return 0 if (m = = 0 ): return 1 return ((n - m) * eulerian(n - 1 , m - 1 ) + (m + 1 ) * eulerian(n - 1 , m)) # Driver code n = 3 m = 1 print (eulerian(n, m)) # This code is contributed by rishabh_jain |
C#
// C# program to find Eulerian number A(n, m) using System; class Eulerian { // Return eulerian number A(n, m) public static int eulerian( int n, int m) { if (m >= n || n == 0) return 0; if (m == 0) return 1; return (n - m) * eulerian(n - 1, m - 1) + (m + 1) * eulerian(n - 1, m); } // driver code public static void Main() { int n = 3, m = 1; Console.WriteLine(eulerian(n, m)); } } // This code is contributed by vt_m |
PHP
<?php // PHP Program to find // Eulerian number A(n, m) // Return euleriannumber A(n, m) function eulerian( $n , $m ) { if ( $m >= $n || $n == 0) return 0; if ( $m == 0) return 1; return ( $n - $m ) * eulerian( $n - 1, $m - 1) + ( $m + 1) * eulerian( $n - 1, $m ); } // Driven Code $n = 3; $m = 1; echo eulerian( $n , $m ); // This code is contributed by anuj_67. ?> |
Javascript
<script> // JavaScript Program to find Eulerian number A(n, m) // Return eulerian number A(n, m) function eulerian(n, m) { if (m >= n || n == 0) return 0; if (m == 0) return 1; return (n - m) * eulerian(n - 1, m - 1) + (m + 1) * eulerian(n - 1, m); } // Driver code let n = 3, m = 1; document.write( eulerian(n, m) ); </script> |
4
Time Complexity: O(2n)
Auxiliary Space: O(log(n)), Due, to recursive call stack
Below is the implementation of finding A(n, m) using Dynamic Programming:
C++
// CPP Program to find Eulerian number A(n, m) #include <bits/stdc++.h> using namespace std; // Return euleriannumber A(n, m) int eulerian( int n, int m) { int dp[n + 1][m + 1]; memset (dp, 0, sizeof (dp)); // For each row from 1 to n for ( int i = 1; i <= n; i++) { // For each column from 0 to m for ( int j = 0; j <= m; j++) { // If i is greater than j if (i > j) { // If j is 0, then make that // state as 1. if (j == 0) dp[i][j] = 1; // basic recurrence relation. else dp[i][j] = ((i - j) * dp[i - 1][j - 1]) + ((j + 1) * dp[i - 1][j]); } } } return dp[n][m]; } // Driven Program int main() { int n = 3, m = 1; cout << eulerian(n, m) << endl; return 0; } |
Java
// Java program to find Eulerian number A(n, m) import java.util.*; class Eulerian { // Return euleriannumber A(n, m) public static int eulerian( int n, int m) { int [][] dp = new int [n + 1 ][m + 1 ]; // For each row from 1 to n for ( int i = 1 ; i <= n; i++) { // For each column from 0 to m for ( int j = 0 ; j <= m; j++) { // If i is greater than j if (i > j) { // If j is 0, then make // that state as 1. if (j == 0 ) dp[i][j] = 1 ; // basic recurrence relation. else dp[i][j] = ((i - j) * dp[i - 1 ][j - 1 ]) + ((j + 1 ) * dp[i - 1 ][j]); } } } return dp[n][m]; } // driver code public static void main(String[] args) { int n = 3 , m = 1 ; System.out.print(eulerian(n, m)); } } // This code is contributed by rishabh_jain |
Python3
# Python3 Program to find Eulerian # number A(n, m) # Return euleriannumber A(n, m) def eulerian(n, m): dp = [[ 0 for x in range (m + 1 )] for y in range (n + 1 )] # For each row from 1 to n for i in range ( 1 , n + 1 ): # For each column from 0 to m for j in range ( 0 , m + 1 ): # If i is greater than j if (i > j): # If j is 0, then make that # state as 1. if (j = = 0 ): dp[i][j] = 1 # basic recurrence relation. else : dp[i][j] = (((i - j) * dp[i - 1 ][j - 1 ]) + ((j + 1 ) * dp[i - 1 ][j])) return dp[n][m] # Driven Program n = 3 m = 1 print (eulerian(n, m)) # This code is contributed by Prasad Kshirsagar |
C#
// C# program to find Eulerian number A(n, m) using System; class Eulerian { // Return euleriannumber A(n, m) public static int eulerian( int n, int m) { int [, ] dp = new int [n + 1, m + 1]; // For each row from 1 to n for ( int i = 1; i <= n; i++) { // For each column from 0 to m for ( int j = 0; j <= m; j++) { // If i is greater than j if (i > j) { // If j is 0, then make // that state as 1. if (j == 0) dp[i, j] = 1; // basic recurrence relation. else dp[i, j] = ((i - j) * dp[i - 1, j - 1]) + ((j + 1) * dp[i - 1, j]); } } } return dp[n, m]; } // driver code public static void Main() { int n = 3, m = 1; Console.WriteLine(eulerian(n, m)); } } // This code is contributed by vt_m |
PHP
<?php // PHP Program to find Eulerian // number A(n, m) // Return euleriannumber A(n, m) function eulerian( $n , $m ) { $dp = array ( array ()); for ( $i = 0; $i < $n + 1; $i ++) for ( $j = 0; $j < $m + 1; $j ++) $dp [ $i ][ $j ] = 0 ; // For each row from 1 to n for ( $i = 1; $i <= $n ; $i ++) { // For each column from 0 to m for ( $j = 0; $j <= $m ; $j ++) { // If i is greater than j if ( $i > $j ) { // If j is 0, then make that // state as 1. if ( $j == 0) $dp [ $i ][ $j ] = 1; // basic recurrence relation. else $dp [ $i ][ $j ] = (( $i - $j ) * $dp [ $i - 1][ $j - 1]) + (( $j + 1) * $dp [ $i - 1][ $j ]); } } } return $dp [ $n ][ $m ]; } // Driver Code $n = 3 ; $m = 1; echo eulerian( $n , $m ) ; // This code is contributed by Ryuga ?> |
Javascript
<script> // Javascript Program to find // Eulerian number A(n, m) // Return euleriannumber A(n, m) function eulerian(n, m) { var dp = Array.from(Array(n+1), ()=> Array(m+1).fill(0)); // For each row from 1 to n for ( var i = 1; i <= n; i++) { // For each column from 0 to m for ( var j = 0; j <= m; j++) { // If i is greater than j if (i > j) { // If j is 0, then make that // state as 1. if (j == 0) dp[i][j] = 1; // basic recurrence relation. else dp[i][j] = ((i - j) * dp[i - 1][j - 1]) + ((j + 1) * dp[i - 1][j]); } } } return dp[n][m]; } // Driven Program var n = 3, m = 1; document.write( eulerian(n, m) ); </script> |
4
Time Complexity: O(n*m)
Auxiliar Space: O(n*m)
Efficient approach: Space optimization
In the 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 m+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 variable prev to store previous computations and a temp variable to update prev at every iteration.
- After every iteration assign the value of temp to prev for further iteration.
- At last return and print the final answer stored in dp[m].
Implementation:
C++
// CPP Program to find Eulerian number A(n, m) #include <bits/stdc++.h> using namespace std; // Return euleriannumber A(n, m) int eulerian( int n, int m) { vector< int > dp(m+1,0); // For each row from 1 to n for ( int i = 1; i <= n; i++) { int prev = 0; // For each column from 0 to m for ( int j = 0; j <= m; j++) { int temp = dp[j]; // If i is greater than j if (i > j) { // If j is 0, then make that // state as 1. if (j == 0) dp[j] = 1; // basic recurrence relation. else dp[j] = ((i - j) * prev) + ((j + 1) * dp[j]); prev = temp; } } } return dp[m]; } // Driven Program int main() { int n = 3, m = 1; cout << eulerian(n, m) << endl; return 0; } |
Java
import java.util.*; public class Main { // Return euleriannumber A(n, m) static int eulerian( int n, int m) { int [] dp = new int [m+ 1 ]; Arrays.fill(dp, 0 ); // For each row from 1 to n for ( int i = 1 ; i <= n; i++) { int prev = 0 ; // For each column from 0 to m for ( int j = 0 ; j <= m; j++) { int temp = dp[j]; // If i is greater than j if (i > j) { // If j is 0, then make that state as 1. if (j == 0 ) dp[j] = 1 ; // basic recurrence relation. else dp[j] = ((i - j) * prev) + ((j + 1 ) * dp[j]); prev = temp; } } } return dp[m]; } // Driven Program public static void main(String[] args) { int n = 3 , m = 1 ; System.out.println(eulerian(n, m)); } } |
Python3
# Function to find Eulerian number A(n, m) def eulerian(n, m): dp = [ 0 ] * (m + 1 ) # For each row from 1 to n for i in range ( 1 , n + 1 ): prev = 0 # For each column from 0 to m for j in range ( 0 , m + 1 ): temp = dp[j] # If i is greater than j if i > j: # If j is 0, then make that # state as 1. if j = = 0 : dp[j] = 1 # Basic recurrence relation. else : dp[j] = ((i - j) * prev) + ((j + 1 ) * dp[j]) prev = temp return dp[m] # Driver Code n = 3 m = 1 print (eulerian(n, m)) |
Javascript
// Function to find Eulerian number A(n, m) function eulerian(n, m) { let dp = new Array(m + 1).fill(0); // For each row from 1 to n for (let i = 1; i <= n; i++) { let prev = 0; // For each column from 0 to m for (let j = 0; j <= m; j++) { let temp = dp[j]; // If i is greater than j if (i > j) { // If j is 0, then make that state as 1. if (j == 0) { dp[j] = 1; } // Basic recurrence relation. else { dp[j] = (i - j) * prev + (j + 1) * dp[j]; } prev = temp; } } } return dp[m]; } // Driver Code let n = 3; let m = 1; console.log(eulerian(n, m)); |
4
Time Complexity: O(n*m)
Auxiliary Space: O(m)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!