# Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal

• Last Updated : 15 Sep, 2021

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).

Examples:

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 4
Output: 5

Input: N = 3
Output: 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:

## C++

 `// C++ Program to implement``// the above approach` `#include ``using` `namespace` `std;` `// Function to calculate``// Binomial Coefficient C(n, r)``int` `binCoff(``int` `n, ``int` `r)``{``    ``int` `val = 1;``    ``int` `i;``    ``if` `(r > (n - r)) {` `        ``// C(n, r) = C(n, n-r)``        ``r = (n - r);``    ``}` `    ``for` `(i = 0; i < r; i++) {` `        ``// [n * (n-1) *---* (n-r+1)] /``        ``// [r * (r-1) *----* 1]``        ``val *= (n - i);``        ``val /= (i + 1);``    ``}``    ``return` `val;``}``// Function to calculate``// the total possible paths``int` `findWays(``int` `n)``{``    ``// Update n to n - 1 as (N - 1)``    ``// catalan number is the result``    ``n--;` `    ``int` `a, b, ans;` `    ``// Stores 2nCn``    ``a = binCoff(2 * n, n);` `    ``// Stores Nth Catalan``    ``// number``    ``b = a / (n + 1);` `    ``// Stores the required``    ``// answer``    ``ans = b;` `    ``return` `ans;``}` `// Driver Code``int` `main()``{` `    ``int` `n = 4;``    ``cout << findWays(n);` `    ``return` `0;``}`

## Java

 `// Java Program to implement``// the above approach``import` `java.util.*;``class` `GFG {` `    ``// Function to calculate``    ``// Binomial Coefficient C(n, r)``    ``static` `int` `binCoff(``int` `n, ``int` `r)``    ``{``        ``int` `val = ``1``;``        ``int` `i;``        ``if` `(r > (n - r)) {``            ``// C(n, r) = C(n, n-r)``            ``r = (n - r);``        ``}` `        ``for` `(i = ``0``; i < r; i++) {``            ``// [n * (n - 1) *---* (n - r + 1)] /``            ``// [r * (r - 1) *----* 1]``            ``val *= (n - i);``            ``val /= (i + ``1``);``        ``}``        ``return` `val;``    ``}` `    ``// Function to calculate``    ``// the total possible paths``    ``static` `int` `findWays(``int` `n)``    ``{``        ``// Update n to n - 1 as (N - 1)``        ``// catalan number is the result``        ``n--;` `        ``int` `a, b, ans;` `        ``// Stores 2nCn``        ``a = binCoff(``2` `* n, n);` `        ``// Stores Nth Catalan``        ``// number``        ``b = a / (n + ``1``);` `        ``// Stores the required``        ``// answer``        ``ans = b;` `        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int` `n = ``4``;``        ``System.out.print(findWays(n));``    ``}``}` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 Program to implement``# the above approach``# Function to calculate``# Binomial Coefficient C(n, r)``def` `binCoff(n, r):``    ``val ``=` `1``    ``if` `(r > (n ``-` `r)):` `        ``# C(n, r) = C(n, n-r)``        ``r ``=` `(n ``-` `r)` `    ``for` `i ``in` `range` `(r):` `        ``# [n * (n-1) *---* (n-r + 1)] /``        ``# [r * (r-1) *----* 1]``        ``val ``*``=` `(n ``-` `i)``        ``val ``/``/``=` `(i ``+` `1``)``    ``return` `val``  ` `# Function to calculate``# the total possible paths``def` `findWays(n):` `    ``# Update n to n - 1``    ``n ``=` `n ``-` `1` `    ``# Stores 2nCn``    ``a ``=` `binCoff(``2` `*` `n, n)` `    ``# Stores Nth Catalan``    ``# number``    ``b ``=` `a ``/``/` `(n ``+` `1``)` `    ``# Stores the required``    ``# answer``    ``ans ``=` `b` `    ``return` `ans` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``  ` `    ``n ``=` `4``    ``print``(findWays(n))` `# This code is contributed by Chitranayal`

## C#

 `// C# Program to implement``// the above approach``using` `System;``class` `GFG {` `    ``// Function to calculate``    ``// Binomial Coefficient C(n, r)``    ``static` `int` `binCoff(``int` `n, ``int` `r)``    ``{``        ``int` `val = 1;``        ``int` `i;``        ``if` `(r > (n - r)) {``            ``// C(n, r) = C(n, n-r)``            ``r = (n - r);``        ``}` `        ``for` `(i = 0; i < r; i++) {``            ``// [n * (n - 1) *---* (n - r + 1)] /``            ``// [r * (r - 1) *----* 1]``            ``val *= (n - i);``            ``val /= (i + 1);``        ``}``        ``return` `val;``    ``}` `    ``// Function to calculate``    ``// the total possible paths``    ``static` `int` `findWays(``int` `n)``    ``{``        ``// Update n to n - 1 as (N - 1)``        ``// catalan number is the result``        ``n--;` `        ``int` `a, b, ans;` `        ``// Stores 2nCn``        ``a = binCoff(2 * n, n);` `        ``// Stores Nth Catalan``        ``// number``        ``b = a / (n + 1);` `        ``// Stores the required``        ``// answer``        ``ans = 2 * b;` `        ``return` `ans;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `Main(String[] args)``    ``{``        ``int` `n = 4;``        ``Console.Write(findWays(n));``    ``}``}` `// This code is contributed by shikhasingrajput`

## Javascript

 ``
Output:
`5`

My Personal Notes arrow_drop_up