Given four positive integers M, N, X, and Y, the task is to count all the possible ways to reach from the top left(i.e., (0, 0)) to the bottom right (M, N) of a matrix of size (M+1)x(N+1) without visiting the cell (X, Y). It is given that from each cell (i, j) you can either move only to right (i, j + 1) or down (i + 1, j).
Â
Examples:Â Â
Input: M = 2, N = 2, X = 1, Y = 1Â
Output: 2Â
Explanation:Â
There are only 2 ways to reach (2, 2) without visiting (1, 1) and the two paths are:Â
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2)Â
(0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2)Input: M = 5, N = 4, X = 3, Y = 2Â
Output: 66Â
Explanation:Â
There are 66 ways to reach (5, 4) without visiting (3, 2). Â
Approach:
To solve the problem mentioned above the idea is to subtract the number of ways to reach from (0, 0) to (X, Y) which was followed by reaching (M, N) from (X, Y) by visiting (X, Y) from the total number of ways reaching (M, N) from (0, 0).Â
Therefore,Â
Â
- The number of ways to reach from (M, N) from the origin (0, 0) is given by:Â
Â
- The number of ways to reach (M, N) only by visiting (X, Y) is reaching (X, Y) from (0, 0) which was followed by reaching (M, N) from (X, Y) is given by:Â
Â
ÂTherefore,Â
Â
Â
- Hence, the equation for the total number of ways are:Â
Â
Below is the implementation of the above approach:Â
Â
C++
// C++ program from the above approach #include <bits/stdc++.h> using namespace std; Â
int fact( int n); Â
// Function for computing nCr int nCr( int n, int r) { Â Â Â Â return fact(n) Â Â Â Â Â Â Â Â Â Â Â / (fact(r) * fact(n - r)); } Â
// Function to find factorial of a number int fact( int n) { Â Â Â Â int res = 1; Â
    for ( int i = 2; i <= n; i++)         res = res * i; Â
    return res; } Â
// Function for counting the number // of ways to reach (m, n) without // visiting (x, y) int countWays( int m, int n, int x, int y) {     return nCr(m + n, m)            - nCr(x + y, x) * nCr(m + n                                      - x - y,                                  m - x); } Â
// Driver Code int main() {     // Given Dimensions of Matrix     int m = 5;     int n = 4; Â
    // Cell not to be visited     int x = 3;     int y = 2; Â
    // Function Call     cout << countWays(m, n, x, y);     return 0; } |
Java
// Java program from the above approach    import java.util.*;    class GFG{        // Function for computing nCr    public static int nCr( int n, int r)        {        return fact(n) / (fact(r) * fact(n - r));        }             // Function to find factorial of a number    public static int fact( int n)    {        int res = 1 ;          for ( int i = 2 ; i <= n; i++)                res = res * i;            return res;        }             // Function for counting the number        // of ways to reach (m, n) without        // visiting (x, y)        public static int countWays( int m, int n,                             int x, int y)        {        return nCr(m + n, m) -            nCr(x + y, x) *            nCr(m + n - x - y, m - x);        } Â
// Driver code public static void main(String[] args) {             // Given Dimensions of Matrix        int m = 5 ;            int n = 4 ;                         // Cell not to be visited        int x = 3 ;            int y = 2 ;                         // Function Call        System.out.println(countWays(m, n, x, y));    }    } Â
// This code is contributed by divyeshrabadiya07 |
Python3
# Python3 program for the above approach Â
# Function for computing nCr def nCr(n, r): Â Â Â Â Â Â Â Â Â return (fact(n) / / (fact(r) * Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â fact(n - r))) Â
# Function to find factorial of a number def fact(n): Â Â Â Â Â Â Â Â Â res = 1 Â Â Â Â for i in range ( 2 , n + 1 ): Â Â Â Â Â Â Â Â res = res * i Â
    return res Â
# Function for counting the number # of ways to reach (m, n) without # visiting (x, y) def countWays(m, n, x, y): Â Â Â Â Â Â Â Â Â return (nCr(m + n, m) - nCr(x + y, x) * Â Â Â Â Â Â Â Â Â Â Â Â nCr(m + n - x - y, m - x)) Â
# Driver Code Â
# Given dimensions of Matrix m = 5 n = 4 Â
# Cell not to be visited x = 3 y = 2 Â
# Function call print (countWays(m, n, x, y)) Â
# This code is contributed by sanjoy_62 |
C#
// C# program from the above approach    using System; Â
class GFG{      // Function for computing nCr    public static int nCr( int n, int r)        {        return fact(n) / (fact(r) * fact(n - r));        }             // Function to find factorial of a number    public static int fact( int n)    {        int res = 1;          for ( int i = 2; i <= n; i++)                res = res * i;              return res;        }             // Function for counting the number        // of ways to reach (m, n) without        // visiting (x, y)        public static int countWays( int m, int n,                             int x, int y)        {        return nCr(m + n, m) -            nCr(x + y, x) *            nCr(m + n - x - y, m - x);        } Â
// Driver code public static void Main(String[] args) {             // Given dimensions of Matrix        int m = 5;            int n = 4;                         // Cell not to be visited        int x = 3;            int y = 2;                         // Function call        Console.WriteLine(countWays(m, n, x, y));    }    } Â
// This code is contributed by Rajput-Ji |
Javascript
<script> Â
Â
// Javascript Program to implement // the above approach Â
// Function for computing nCr    function nCr(n, r)        {        return fact(n) / (fact(r) * fact(n - r));        }               // Function to find factorial of a number    function fact(n)    {        let res = 1;            for (let i = 2; i <= n; i++)                res = res * i;            return res;        }               // Function for counting the number        // of ways to reach (m, n) without        // visiting (x, y)        function countWays(m, n, x, y)        {        return nCr(m + n, m) -            nCr(x + y, x) *            nCr(m + n - x - y, m - x);        } Â
// Driver Code          // Given Dimensions of Matrix        let m = 5;            let n = 4;                           // Cell not to be visited        let x = 3;            let y = 2;                           // Function Call        document.write(countWays(m, n, x, y)); Â
// This code is contributed by avijitmondal1998. </script> |
66
Time Complexity: O(M + N), where M, N represents the size of the matrix.
Auxiliary Space: O(1), as constant space is required.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!