Given an integer N which denotes the size of a matrix, the task is to find the number of possible ways to reach the bottom-right corner from the top-left corner of the matrix without crossing the diagonal of the matrix. The possible movements from any cell (i, j) from the matrix are (i, j + 1) (Right) or (i + 1, j) (Down).
Input: N = 4
Input: N = 3
Approach: The problem can be solved based on the following observation:
- The allowed movements in the matrix are one cell downwards or rightwards without crossing the diagonal.
- Therefore, at any point, the number of downward moves will always be greater than or equal to the number of rightward moves.
- Therefore, this follows the pattern of Catalan Numbers.
Therefore, based on the observation, the problem reduces to calculating Nth Catalan Number. The path calculated for only upper triangle is considered because crossing of diagonal is not allowed. If there is a movement from cell (0, 0) to (1, 0) will result in crossing of diagonal.
Nth Catalan Number (Kn) = (2NCN )/(N + 1), where 2nCn is binomial coefficient.
Total number of ways = Kn
Below is the implementation of the above approach:
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.
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
- Count all possible paths from top left to bottom right of a mXn matrix
- Print all possible paths from top left to bottom right of a mXn matrix
- Print all palindromic paths from top left to bottom right in a matrix
- Print all paths from top left to bottom right in a matrix with four moves allowed
- Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
- Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
- Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
- Maximum points from top left of matrix to bottom right and return back
- Lexicographically largest prime path from top-left to bottom-right in a matrix
- Minimum cost to reach from the top-left to the bottom-right corner of a matrix
- Maximum sum path in a matrix from top-left to bottom-right
- Maximum XOR of a path from top-left to bottom-right cell of given Matrix
- Maximize trailing zeros in product from top left to bottom right of given Matrix
- Print a matrix in alternate manner (left to right then right to left)
- Kth diagonal from the top left of a given matrix
- Find maximum sum from top to bottom row with no adjacent diagonal elements
- Number of shortest paths to reach every cell from bottom-left cell in the grid
- Check if string is right to left diagonal or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.