Motzkin number
Last Updated :
10 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int motzkin( int n)
{
if (n == 0 || n == 1)
return 1;
return ((2 * n + 1) * motzkin(n - 1) +
(3 * n - 3) * motzkin(n - 2)) / (n + 2);
}
int main()
{
int n = 8;
cout << motzkin(n) << endl;
return 0;
}
|
Java
import java.util.*;
class Digits
{
public static int motzkin( int n)
{
if (n == 0 || n == 1 )
return 1 ;
return (( 2 * n + 1 ) * motzkin(n - 1 ) +
( 3 * n - 3 ) * motzkin(n - 2 )) / (n + 2 );
}
public static void main(String[] args)
{
int n = 8 ;
System.out.print( motzkin(n) );
}
}
|
Python3
def motzkin(n) :
if (n = = 0 or n = = 1 ) :
return 1
return (( 2 * n + 1 ) * motzkin(n - 1 ) +
( 3 * n - 3 ) * motzkin(n - 2 )) / (n + 2 )
n = 8
print ( motzkin(n) )
|
C#
using System;
class GFG {
public static int motzkin( int n)
{
if (n == 0 || n == 1)
return 1;
return ((2 * n + 1) * motzkin(n - 1) +
(3 * n - 3) * motzkin(n - 2)) / (n + 2);
}
public static void Main()
{
int n = 8;
Console.WriteLine( motzkin(n) );
}
}
|
PHP
<?php
function motzkin( $n )
{
if ( $n == 0 || $n == 1)
return 1;
return ((2 * $n + 1) *
motzkin( $n - 1) +
(3 * $n - 3) *
motzkin( $n - 2)) /
( $n + 2);
}
$n = 8;
echo (motzkin( $n ));
?>
|
Javascript
<script>
function motzkin( n)
{
if (n == 0 || n == 1)
return 1;
return ((2 * n + 1) * motzkin(n - 1) +
(3 * n - 3) * motzkin(n - 2)) / (n + 2);
}
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++
#include <bits/stdc++.h>
using namespace std;
int motzkin( int n)
{
int dp[n + 1];
dp[0] = dp[1] = 1;
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];
}
int main()
{
int n = 8;
cout << motzkin(n) << endl;
return 0;
}
|
Java
import java.util.*;
class Digits
{
public static int motzkin( int n)
{
int [] dp = new int [n+ 1 ];
dp[ 0 ] = dp[ 1 ] = 1 ;
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];
}
public static void main(String[] args)
{
int n = 8 ;
System.out.print( motzkin(n) );
}
}
|
Python3
def motzkin(n) :
dp = [ None ] * (n + 1 )
dp[ 0 ] = dp[ 1 ] = 1 ;
i = 2
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];
n = 8
print ( motzkin(n) )
|
C#
using System;
class GFG {
public static int motzkin( int n)
{
int [] dp = new int [n+1];
dp[0] = dp[1] = 1;
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];
}
public static void Main()
{
int n = 8;
Console.WriteLine( motzkin(n) );
}
}
|
PHP
<?php
function motzkin( $n )
{
$dp [0] = $dp [1] = 1;
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 ];
}
$n = 8;
echo (motzkin( $n ));
?>
|
Javascript
function motzkin(n)
{
let dp = new Array(n+1).fill(0);
dp[0] = dp[1] = 1;
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];
}
let n = 8;
console.log(motzkin(n));
|
Output:
323
Time complexity: O(n)
space complexity: O(n)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...