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. Therefore No. of ways = 10! / (5! * 5!) i.e 252 ways.
Please Login to comment...