Number of ways to go from one point to another in a grid

16 Apr, 2021
Given the NxN grid of horizontal and vertical roads. The task is to find out the number of ways that the person can go from point A to point B using the shortest possible path.Note: A and B point are fixed i.e A is at top left corner and B at bottom right corner as shown in the below image. 
In the above image, the path shown in the red and light green colour are the two possible paths to reach from point A to point B.Examples:  
Input: N = 3
Output: Ways = 20

Input: N = 4
Output: Ways = 70
 
Formula: Let the grid be N x N, number of ways can be written as.  
How does above formula work? Let consider the example of the 5×5 grid as shown above. In order to go from point A to point B in the 5×5 grid, We have to take 5 horizontal steps and 5 vertical steps. Each path will be an arrangement of 10 steps out of which 5 steps are identical of one kind and other 5 steps are identical of a second kind. ThereforeNo. of ways = 10! / (5! * 5!) i.e 252 ways. 
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; // function that will// calculate the factoriallong factorial(int N){    int result = 1;    while (N > 0) {        result = result * N;        N--;    }    return result;} long countWays(int N){    long total = factorial(N + N);    long total1 = factorial(N);    return (total / total1) / total1;} // Driver codeint main(){    int N = 5;    cout << "Ways = " << countWays(N);    return 0;}
// Java implementation of above approachclass GfG {     // function that will    // calculate the factorial    static long factorial(int N)    {        int result = 1;        while (N > 0) {            result = result * N;            N--;        }        return result;    }     static long countWays(int N)    {        long total = factorial(N + N);        long total1 = factorial(N);        return (total / total1) / total1;    }     // Driver code    public static void main(String[] args)    {        int N = 5;        System.out.println("Ways = " + countWays(N));    }}
# Python3 implementation of above approach # function that will calculate the factorialdef factorial(N) :         result = 1;         while (N > 0) :                 result = result * N;        N -= 1;         return result; def countWays(N) :     total = factorial(N + N);    total1 = factorial(N);         return (total // total1) // total1; # Driver codeif __name__ == "__main__" :     N = 5;         print("Ways =", countWays(N)); # This code is contributed by Ryuga
// C# implementation of above approachusing System;class GfG{     // function that will    // calculate the factorial    static long factorial(int N)    {        int result = 1;        while (N > 0)        {            result = result * N;            N--;        }        return result;    }     static long countWays(int N)    {        long total = factorial(N + N);        long total1 = factorial(N);        return (total / total1) / total1;    }     // Driver code    public static void Main(String []args)    {        int N = 5;        Console.WriteLine("Ways = " + countWays(N));    }} // This code is contributed by Arnab Kundu
<?php// PHP implementation of above approach // function that will// calculate the factorialfunction factorial($N){    $result = 1;    while ($N > 0)    {        $result = $result * $N;        $N--;    }    return $result;} function countWays($N){    $total = factorial($N + $N);    $total1 = factorial($N);    return ($total / $total1) / $total1;} // Driver code$N = 5;echo "Ways = ", countWays($N);     // This code is contributed by ajit?>
<script> // Javascript implementation of above approach // function that will// calculate the factorialfunction factorial(N){    var result = 1;    while (N > 0) {        result = result * N;        N--;    }    return result;} function countWays(N){    var total = factorial(N + N);    var total1 = factorial(N);    return (total / total1) / total1;} // Driver codevar N = 5;document.write( "Ways = " + countWays(N)); // This code is contributed by rutvik_56.</script>
Ways = 252
 
prerna saini
andrew1234
ankthon
jit_t
rutvik_56
binomial coefficient
factorial
Marketing
Permutation and Combination
Combinatorial
Combinatorial
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.