Motzkin number
In mathematics, a Motzkin number for a given number n is the number of different ways of drawing non-intersecting chords between n points on a circle (not necessarily touching every point by a chord).
For example, for n = 3, M4 = 9.
Recurrence relation to the Nth Motzkin Number is:
Motzkin Number can be used to find:
- The number of positive integer sequences of length n – 1 in which the opening and ending elements are either 1 or 2, and the difference between any two consecutive elements is -1, 0, or 1.
- The number of routes on the upper right quadrant of a grid from coordinate (0, 0) to coordinate (n, 0) in n steps if one is allowed to move only to the right (up, down or straight) at each step but forbidden from dipping below the y = 0 axis.
For example–
The following figure shows the 9 valid Motzkin paths from (0, 0) to (4, 0).
Examples:
Input : n = 4 Output : 9 Input : n = 5 Output : 21
Below is the program to find nth Motzkin Number:
C++
// CPP Program to find Nth Motzkin Number. #include <bits/stdc++.h> using namespace std; // Return the nth Motzkin Number. int motzkin( int n) { // Base Case if (n == 0 || n == 1) return 1; // Recursive step return ((2 * n + 1) * motzkin(n - 1) + (3 * n - 3) * motzkin(n - 2)) / (n + 2); } // Driven Program int main() { int n = 8; cout << motzkin(n) << endl; return 0; } |
Java
// Java Program to find Nth Motzkin Number. import java.util.*; class Digits { // Return the nth Motzkin Number. public static int motzkin( int n) { // Base Case if (n == 0 || n == 1 ) return 1 ; // Recursive step return (( 2 * n + 1 ) * motzkin(n - 1 ) + ( 3 * n - 3 ) * motzkin(n - 2 )) / (n + 2 ); } // driver code public static void main(String[] args) { int n = 8 ; System.out.print( motzkin(n) ); } } // This code is contributed by rishabh_jain |
Python3
# Python3 program to find Nth Motzkin Number. # Return the nth Motzkin Number. def motzkin(n) : # Base Case if (n = = 0 or n = = 1 ) : return 1 # Recursive step return (( 2 * n + 1 ) * motzkin(n - 1 ) + ( 3 * n - 3 ) * motzkin(n - 2 )) / (n + 2 ) # Driver code n = 8 print ( motzkin(n) ) # This code is contributed by rishabh_jain |
C#
// C# Program to find Nth Motzkin Number. using System; class GFG { // Return the nth Motzkin Number. public static int motzkin( int n) { // Base Case if (n == 0 || n == 1) return 1; // Recursive step return ((2 * n + 1) * motzkin(n - 1) + (3 * n - 3) * motzkin(n - 2)) / (n + 2); } // driver code public static void Main() { int n = 8; Console.WriteLine( motzkin(n) ); } } // This code is contributed by vt_m |
PHP
<?php // PHP Program to find // Nth Motzkin Number. // Return the nth Motzkin Number. function motzkin( $n ) { // Base Case if ( $n == 0 || $n == 1) return 1; // Recursive step return ((2 * $n + 1) * motzkin( $n - 1) + (3 * $n - 3) * motzkin( $n - 2)) / ( $n + 2); } // Driven Code $n = 8; echo (motzkin( $n )); // This code is contributed by Ajit. ?> |
Javascript
<script> // javascript Program to find Nth Motzkin Number. // Return the nth Motzkin Number. function motzkin( n) { // Base Case if (n == 0 || n == 1) return 1; // Recursive step return ((2 * n + 1) * motzkin(n - 1) + (3 * n - 3) * motzkin(n - 2)) / (n + 2); } // driver code var n = 8; document.write( motzkin(n) ); </script> |
Output :
323
Time complexity: O(2n)
space complexity: O(2n)
Using Dynamic Programming :
Below is the Dynamic Programming solution of finding nth Motzkin Number :
C++
// CPP Program to find Nth Motzkin Number. #include <bits/stdc++.h> using namespace std; // Return the nth Motzkin Number. int motzkin( int n) { int dp[n + 1]; // Base case dp[0] = dp[1] = 1; // Finding i-th Motzkin number. for ( int i = 2; i <= n; i++) dp[i] = ((2 * i + 1) * dp[i - 1] + (3 * i - 3) * dp[i - 2]) / (i + 2); return dp[n]; } // Driven Program int main() { int n = 8; cout << motzkin(n) << endl; return 0; } |
Java
// Java Program to find Nth Motzkin Number. import java.util.*; class Digits { // Return the nth Motzkin Number. public static int motzkin( int n) { int [] dp = new int [n+ 1 ]; // Base case dp[ 0 ] = dp[ 1 ] = 1 ; // Finding i-th Motzkin number. for ( int i = 2 ; i <= n; i++) dp[i] = (( 2 * i + 1 ) * dp[i - 1 ] + ( 3 * i - 3 ) * dp[i - 2 ]) / (i + 2 ); return dp[n]; } // driver code public static void main(String[] args) { int n = 8 ; System.out.print( motzkin(n) ); } } // This code is contributed by rishabh_jain |
Python3
# Python3 program to find Nth Motzkin Number. # Return the nth Motzkin Number. def motzkin(n) : dp = [ None ] * (n + 1 ) # Base case dp[ 0 ] = dp[ 1 ] = 1 ; i = 2 # Finding i-th Motzkin number. while i < = n : dp[i] = (( 2 * i + 1 ) * dp[i - 1 ] + ( 3 * i - 3 ) * dp[i - 2 ]) / (i + 2 ); i = i + 1 return dp[n]; # Driver code n = 8 print ( motzkin(n) ) # This code is contributed by rishabh_jain |
C#
// C# Program to find Nth Motzkin Number. using System; class GFG { // Return the nth Motzkin Number. public static int motzkin( int n) { int [] dp = new int [n+1]; // Base case dp[0] = dp[1] = 1; // Finding i-th Motzkin number. for ( int i = 2; i <= n; i++) dp[i] = ((2 * i + 1) * dp[i - 1] + (3 * i - 3) * dp[i - 2]) / (i + 2); return dp[n]; } // driver code public static void Main() { int n = 8; Console.WriteLine( motzkin(n) ); } } // This code is contributed by vt_m |
PHP
<?php // PHP Program to find // Nth Motzkin Number. // Return the nth Motzkin Number. function motzkin( $n ) { // Base case $dp [0] = $dp [1] = 1; // Finding i-th Motzkin number. for ( $i = 2; $i <= $n ; $i ++) $dp [ $i ] = ((2 * $i + 1) * $dp [ $i - 1] + (3 * $i - 3) * $dp [ $i - 2]) / ( $i + 2); return $dp [ $n ]; } // Driven Code $n = 8; echo (motzkin( $n )); // This code is contributed by Ajit. ?> |
Javascript
// JavaScript Program to find Nth Motzkin Number. // Return the nth Motzkin Number. function motzkin(n) { let dp = new Array(n+1).fill(0); // Base case dp[0] = dp[1] = 1; // Finding i-th Motzkin number. for (let i = 2; i <= n; i++) dp[i] = ((2 * i + 1) * dp[i - 1] + (3 * i - 3) * dp[i - 2]) / (i + 2); return dp[n]; } // Driven Program let n = 8; console.log(motzkin(n)); // The code is contributed by Nidhi goel |
Output:
323
Time complexity: O(n)
space complexity: O(n)
Please Login to comment...