Motzkin number | CPP Program to find Nth Motzkin Number . ; Return the nth Motzkin Number . ; Base case ; Finding i - th Motzkin number . ; Driven Program
#include <bits/stdc++.h> NEW_LINE 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 ; }