In mathematics, a Delannoy number D describes the number of paths from the southwest corner (0, 0) of a rectangular grid to the northeast corner (m, n), using only single steps north, northeast, or east.
For Example, D(3, 3) equals 63.
Delannoy Number can be calculated by:

Delannoy number can be used to find:
- Counts the number of global alignments of two sequences of lengths m and n.
- Number of points in an m-dimensional integer lattice that are at most n steps from the origin.
- In cellular automata, the number of cells in an m-dimensional von Neumann neighborhood of radius n.
- Number of cells on a surface of an m-dimensional von Neumann neighborhood of radius n.
Examples :
Input : n = 3, m = 3
Output : 63
Input : n = 4, m = 5
Output : 681
Below is the implementation of finding Delannoy Number:
C++
#include <bits/stdc++.h>
using namespace std;
int dealnnoy( int n, int m)
{
if (m == 0 || n == 0)
return 1;
return dealnnoy(m - 1, n) +
dealnnoy(m - 1, n - 1) +
dealnnoy(m, n - 1);
}
int main()
{
int n = 3, m = 4;
cout << dealnnoy(n, m) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static int dealnnoy( int n, int m)
{
if (m == 0 || n == 0 )
return 1 ;
return dealnnoy(m - 1 , n) +
dealnnoy(m - 1 , n - 1 ) +
dealnnoy(m, n - 1 );
}
public static void main(String args[]){
int n = 3 , m = 4 ;
System.out.println(dealnnoy(n, m));
}
}
|
Python3
def dealnnoy(n, m):
if (m = = 0 or n = = 0 ) :
return 1
return dealnnoy(m - 1 , n) + dealnnoy(m - 1 , n - 1 ) + dealnnoy(m, n - 1 )
n = 3
m = 4 ;
print ( dealnnoy(n, m) )
|
C#
using System;
public class GfG {
public static int dealnnoy( int n, int m)
{
if (m == 0 || n == 0)
return 1;
return dealnnoy(m - 1, n) +
dealnnoy(m - 1, n - 1) +
dealnnoy(m, n - 1);
}
public static void Main()
{
int n = 3, m = 4;
Console.WriteLine(dealnnoy(n, m));
}
}
|
PHP
<?php
function dealnnoy( $n , $m )
{
if ( $m == 0 or $n == 0)
return 1;
return dealnnoy( $m - 1, $n ) +
dealnnoy( $m - 1, $n - 1) +
dealnnoy( $m , $n - 1);
}
$n = 3;
$m = 4;
echo dealnnoy( $n , $m );
?>
|
Output:
129
Below is the Dynamic Programming program to find nth Delannoy Number:
C++
#include <bits/stdc++.h>
using namespace std;
int dealnnoy( int n, int m)
{
int dp[m + 1][n + 1];
for ( int i = 0; i <= m; i++)
dp[i][0] = 1;
for ( int i = 0; i <= m; i++)
dp[0][i] = 1;
for ( int i = 1; i <= m; i++)
for ( int j = 1; j <= n; j++)
dp[i][j] = dp[i - 1][j] +
dp[i - 1][j - 1] +
dp[i][j - 1];
return dp[m][n];
}
int main()
{
int n = 3, m = 4;
cout << dealnnoy(n, m) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int dealnnoy( int n, int m)
{
int dp[][]= new int [m + 1 ][n + 1 ];
for ( int i = 0 ; i <= m; i++)
dp[i][ 0 ] = 1 ;
for ( int i = 0 ; i < m; i++)
dp[ 0 ][i] = 1 ;
for ( int i = 1 ; i <= m; i++)
for ( int j = 1 ; j <= n; j++)
dp[i][j] = dp[i - 1 ][j] +
dp[i - 1 ][j - 1 ] +
dp[i][j - 1 ];
return dp[m][n];
}
public static void main(String args[])
{
int n = 3 , m = 4 ;
System.out.println(dealnnoy(n, m));
}
}
|
Python3
def dealnnoy (n, m):
dp = [[ 0 for x in range (n + 1 )] for x in range (m + 1 )]
for i in range (m):
dp[ 0 ][i] = 1
for i in range ( 1 , m + 1 ):
dp[i][ 0 ] = 1
for i in range ( 1 , m + 1 ):
for j in range ( 1 , n + 1 ):
dp[i][j] = dp[i - 1 ][j] + dp[i - 1 ][j - 1 ] + dp[i][j - 1 ];
return dp[m][n]
n = 3
m = 4
print (dealnnoy(n, m))
|
C#
using System;
class GFG {
static int dealnnoy( int n, int m)
{
int [, ] dp = new int [m + 1, n + 1];
for ( int i = 0; i <= m; i++)
dp[i, 0] = 1;
for ( int i = 0; i < m; i++)
dp[0, i] = 1;
for ( int i = 1; i <= m; i++)
for ( int j = 1; j <= n; j++)
dp[i, j] = dp[i - 1, j]
+ dp[i - 1, j - 1]
+ dp[i, j - 1];
return dp[m, n];
}
public static void Main()
{
int n = 3, m = 4;
Console.WriteLine(dealnnoy(n, m));
}
}
|
PHP
<?php
function dealnnoy( $n , $m )
{
$dp [ $m + 1][ $n + 1] = 0;
for ( $i = 0; $i <= $m ; $i ++)
$dp [ $i ][0] = 1;
for ( $i = 0; $i <= $m ; $i ++)
$dp [0][ $i ] = 1;
for ( $i = 1; $i <= $m ; $i ++)
for ( $j = 1; $j <= $n ; $j ++)
$dp [ $i ][ $j ] = $dp [ $i - 1][ $j ] +
$dp [ $i - 1][ $j - 1] +
$dp [ $i ][ $j - 1];
return $dp [ $m ][ $n ];
}
$n = 3; $m = 4;
echo dealnnoy( $n , $m ) ;
?>
|
Output :
129
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.