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

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:

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to implement
// the above approach
  
#include <bits/stdc++.h>
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 calcualte
// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 calcualte
    // 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 calcualte
# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 calcualte
    // 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

chevron_right


Output:

5

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.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.