Open In App

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:

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




<script>
 
// Javascript Program to implement
// the above approach
 
// Function to calculate
// Binomial Coefficient C(n, r)
function binCoff(n, r)
{
    var val = 1;
    var 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
function findWays(n)
{
    // Update n to n - 1 as (N - 1)
    // catalan number is the result
    n--;
 
    var 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
var n = 4;
document.write( findWays(n));
 
</script>

Output: 
5

 

Time Complexity: O(n)
Auxiliary Space: O(1)


Article Tags :