Open In App

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

Last Updated : 12 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




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




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




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



Similar Reads

Count of possible paths from top left to bottom right of a M x N matrix by moving right, down or diagonally
Given 2 integers M and N, the task is to find the count of all the possible paths from top left to the bottom right of an M x N matrix with the constraints that from each cell you can either move only to right or down or diagonally Examples: Input: M = 3, N = 3Output: 13Explanation: There are 13 paths as follows: VVHH, VHVH, HVVH, DVH, VDH, VHHV, H
14 min read
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths
Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell.Examples: Input: mat[][] = {{1, 2}, {3, 1}} Output: 0 Explanation: Ev
18 min read
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2
Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell. Examples: Input: M = 2, N = 2, mat[M][N] = {{0, 0}, {0, 1}} Output:
12 min read
Print all possible paths from top left to bottom right in matrix
Given a 2D matrix of dimension m✕n, the task is to print all the possible paths from the top left corner to the bottom right corner in a 2D matrix with the constraints that from each cell you can either move to right or down only. Examples : Input: [[1,2,3], [4,5,6]]Output: [[1,4,5,6], [1,2,5,6], [1,2,3,6]] Input: [[1,2], [3,4]]Output: [[1,2,4], [1
8 min read
Minimize count of unique paths from top left to bottom right of a Matrix by placing K 1s
Given two integers N and M where M and N denote a matrix of dimensions N * M consisting of 0's only. The task is to minimize the count of unique paths from the top left (0, 0) to bottom right (N - 1, M - 1) of the matrix across cells consisting of 0's only by placing exactly K 1s in the matrix. Note: Neither the bottom right nor the top-left cell c
10 min read
Print all paths from top left to bottom right in a matrix with four moves allowed
The problem is to print all the possible paths from top left to bottom right of an mXn matrix with the constraints that from each cell you can either move up, right, left or down. Examples: Input : 1 2 3 4 5 6 Output : 1 2 3 6 1 2 5 6 1 4 5 6 4 5 2 3 6 Input : 1 2 3 4 5 6 7 8 9 Output : 1 2 3 6 9 1 2 3 6 5 8 9 1 2 3 6 5 4 7 8 9 1 2 5 6 9 1 2 5 8 9
9 min read
Minimum steps to convert all top left to bottom right paths in Matrix as palindrome | Set 2
Given a matrix mat[][] with N rows and M columns. The task is to find the minimum number of changes required in the matrix such that every path from top left to bottom right is a palindromic path. In a path only right and bottom movements are allowed from one cell to another cell. Examples: Input: mat[][] = {{1, 2}, {3, 1}}Output: 0Explanation:Ever
9 min read
Minimize flips required to make all shortest paths from top-left to bottom-right of a binary matrix equal to S
Given a binary matrix mat[][] having dimensions N * M and a binary string S of length N + M - 1 , the task is to find the minimum number of flips required to make all shortest paths from the top-left cell to the bottom-right cell equal to the given string S. Examples: Input: mat[][] = [[1, 0, 1, 1], [1, 1, 1, 0]], S = "10010"Output: 3 Explanation:
6 min read
Print all palindromic paths from top left to bottom right in a matrix
Given a matrix containing lower alphabetical characters only, we need to print all palindromic paths in given matrix. A path is defined as a sequence of cells starting from top-left cell and ending at bottom-right cell. We are allowed to move to right and down only from current cell. We cannot go down diagonally. Example: Input : mat[][] = {"aaab”,
7 min read
Count paths with maximum sum from top-left to bottom-right cell (Adventure in a Maze)
Given a 2D grid maze[][] of size N * N containing numbers 1, 2 or 3 where 1: means we can go Right from that cell only, 2 means we can go Down from that cell only and 3 means we can go Right and Down to both paths from that cell. We cannot go out of the maze at any time. Find the total number of paths from cell (0, 0) to cell (N - 1, N - 1). There
14 min read