Count all possible paths from top left to bottom right of a Matrix without crossing the diagonal
Last Updated :
12 Oct, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int binCoff( int n, int r)
{
int val = 1;
int i;
if (r > (n - r)) {
r = (n - r);
}
for (i = 0; i < r; i++) {
val *= (n - i);
val /= (i + 1);
}
return val;
}
int findWays( int n)
{
n--;
int a, b, ans;
a = binCoff(2 * n, n);
b = a / (n + 1);
ans = b;
return ans;
}
int main()
{
int n = 4;
cout << findWays(n);
return 0;
}
|
Java
import java.util.*;
class GFG {
static int binCoff( int n, int r)
{
int val = 1 ;
int i;
if (r > (n - r)) {
r = (n - r);
}
for (i = 0 ; i < r; i++) {
val *= (n - i);
val /= (i + 1 );
}
return val;
}
static int findWays( int n)
{
n--;
int a, b, ans;
a = binCoff( 2 * n, n);
b = a / (n + 1 );
ans = b;
return ans;
}
public static void main(String[] args)
{
int n = 4 ;
System.out.print(findWays(n));
}
}
|
Python3
def binCoff(n, r):
val = 1
if (r > (n - r)):
r = (n - r)
for i in range (r):
val * = (n - i)
val / / = (i + 1 )
return val
def findWays(n):
n = n - 1
a = binCoff( 2 * n, n)
b = a / / (n + 1 )
ans = b
return ans
if __name__ = = "__main__" :
n = 4
print (findWays(n))
|
C#
using System;
class GFG {
static int binCoff( int n, int r)
{
int val = 1;
int i;
if (r > (n - r)) {
r = (n - r);
}
for (i = 0; i < r; i++) {
val *= (n - i);
val /= (i + 1);
}
return val;
}
static int findWays( int n)
{
n--;
int a, b, ans;
a = binCoff(2 * n, n);
b = a / (n + 1);
ans = 2 * b;
return ans;
}
public static void Main(String[] args)
{
int n = 4;
Console.Write(findWays(n));
}
}
|
Javascript
<script>
function binCoff(n, r)
{
var val = 1;
var i;
if (r > (n - r)) {
r = (n - r);
}
for (i = 0; i < r; i++) {
val *= (n - i);
val /= (i + 1);
}
return val;
}
function findWays(n)
{
n--;
var a, b, ans;
a = binCoff(2 * n, n);
b = a / (n + 1);
ans = b;
return ans;
}
var n = 4;
document.write( findWays(n));
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...