VOOZH about

URL: https://www.geeksforgeeks.org/dsa/delannoy-number/

⇱ Delannoy Number - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Delannoy Number

Last Updated : 14 Sep, 2023

In mathematics, a 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: 

👁 Image


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: 


Output
129

Below is the Dynamic Programming program to find nth Delannoy Number: 


Output
129

Time complexity: O(m*n)
 space complexity: O(n*m)

Efficient approach: Space optimization

In 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 n+1 and initialize it with 1.
  • Now iterate over subproblems by the help of nested loop and get the current value from previous computations.
  • Now Create a temporary variable prev used to store the previous computations and temp to update prev after 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[n].

Implementation:


Output
129

Time complexity: O(m*n)
Auxiliary space: O(n)

Comment