Skip to content
Related Articles

Related Articles

Maths behind number of paths in matrix problem
  • Difficulty Level : Medium
  • Last Updated : 28 Feb, 2019

The problem is to count all the possible paths from top left to bottom right of an m*n matrix with the constraints that from each cell you can either move only towards right or down.

First of all read various possible solutions for the stated problem here

Now for the last solution, the combination formula for computing number of paths is given as m + n – 2Cm – 1. Let’s discuss the maths behind that formula.

Suppose we have an m*n matrix then according to the question we can only move right or down.

Here m = 5 and n = 3, we start from (0, 0) (i.e. start) and go to the end i.e. (4, 2) we can consider any one path lets say we choose
(0, 0) -> (0, 1) -> (0, 2) -> (1, 2) -> (2, 2) -> (3, 2) -> (4, 2)
Therefore, we moved 2 steps to the right and 4 steps downwards. Even if we take any other path same number of right and down steps will be required.

Now recall the combination in maths. It is just that where instead of letters we have paths. Here we have to cover n-1 + m-1 cellular length to destination.
Also recall that we are moving m-1 steps in downward direction and n-1 in right direction. Therefore the number of paths will essentially be (m + n – 2)! / (n – 1)! * (m – 1)! which is nothing but m + n – 2Cn – 1 or m + n – 2Cm – 1.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up
Recommended Articles
Page :