**Question:** There is an m x n grid. One can only move either down or right at any point in time. One is trying to reach the bottom-right corner of the grid.

How many possible unique paths are there?

Below is an example of 4x4 grid. Here from moving top-left to bottom right corner, we will have 20 unique paths.

1 1 1 1

1 2 3 4

1 3 6 10

1 4 10 20

**Answer:**Since we know that one can only move either down or right at any point in time so one can reach to cell (i,j) only from 2 possible locations:

- One step right from cell (i-1,j)
- One step down from cell (i,j-1)

- Recursion / Backtracking
- Dynamic programming.

Paths[i][j] = paths[i][j-1] + paths[i-1][j]

**Code:**

In code width is representing m and height is representing n.

int no_of_paths(int width, int height)

{

//Creating paths

int** paths = new int*[height];

for(int i = 0; i < height; ++i)

paths[i] = new int[width];

int i,j;

//Initializing paths

for (i=0 ;i< width; i++)

{

for (j=0; j< height; j++)

paths[i][j] = 1;

}

//Calculating paths

for (i=1 ;i< width; i++)

{

for (j=1; j< height; j++)

{

paths[i][j] = paths[i-1][j] + paths[i][j-1];

}

}

//just for printing/debugging purpose

for (i=0 ;i< width; i++)

{

for (j=0; j< height; j++)

{

cout << paths[i][j] << " ";

}

cout << endl;

}

int res = paths[width-1][height-1];

//Deleting paths

for(int i = 0; i < height; ++i) {

delete [] paths[i];

}

delete [] paths;

return res;

}

On a second thought, I see that in place of using a 2D matrix(paths) for memoization, we can just have 1-D array with length as width. Please see the code for the same as below.

int no_of_paths(int width, int height)

{

int i,j;

int* no = new int[width];

for (i=0 ;i< width; i++)

{

no[i] = 1;

cout << "1 "; //just for printing/debugging purpose

}

cout << endl; //just for printing/debugging purpose

for (i=1 ;i< height; i++)

{

cout << no[0] << " "; //just for printing/debugging purpose

for (j=1; j< width; j++)

{

no[j] = no[j]+no[j-1];

cout << no[j] << " "; //just for printing/debugging purpose

}

cout << endl; //just for printing/debugging purpose

}

int res = no[width-1];

delete [] no;

return res;

}

**Complexity:**Since we are traversing each element just once, this solution has linear time complexity.

**Subscribe**- To get an automatic feed of all future posts subscribe here, or to receive them via email go here and enter your email address in the box. You can also like us on facebook and follow me on Twitter @akashag1001.
SaketI approached this problem as follows:

1 . Suppose you are at the top left corner you aim being to reach the bottom right corner.Assuming we have a n*m array.

Initial position [0,0]

Destination [n-1,m-1]

Steps to be taken :right => n-1

Steps to be taken :down => m-1

Now assume the that we have n-1 long sticks and m-1 short sticks which are to be arranged amongst themselves(all long and all short sticks are identical among them selves).

No. of ways of arrangement = (m+n-2)!

But we overcounted !

Similar long sticks => (n-1)!

Simislar short sticks => (m-1)!

Total ways=> (m+n-2)!/(m-1)!*(n-1)!

Akash@Saket: Awesome way for those who love mathematics. Permutation/combination rock.

AnonymousThis isnt exactly an optimisation problem. What do you think we would try to max/min in it when thinking about the problem?

Omar Mohammad OthmanOmar Mohammad Othman[

Saket] What you're proposing is excellent (I actually solved this directly when I heard it because I used to teach Probability and Statistics for undergraduate students when I was a T.A. and this is a well-known rule), but - unless you have a big math library - you'll have to calculate the ((m + n - 2) C (m - 1)) - or equivalently ((m + n - 2) C (n - 1)) - using Pascal's formula, which will also require dynamic programming with exactly the same complexity, runtime and even potential optimizations asAkash's solution. BecauseAkash's is much more intuitive, I prefer to do it that way actually.AnonymousAn intuitive way of understanding the combinatorial solution as follows: At every step we either move right (R) or down (D). So any path is a sequence of Rs and Ds and length of any path from the top to the bottom is n+m. So all possible sequences of Rs and Ds, which equals m+n choose n or m+n choose m, are the total number of paths.