But the Time complexity will be exponential because there will be many sub problems which will be solved again and again to get the final solution. read this : “Dynamic programming vs Recursion

2. Dynamic Programming (Better Solution)

Create two dimensional resultCount array to store the number of paths from top left corner.

Base Case: To reach to any cell in either first row or column from first cell(top left at 0,0) will be 1.

You can reach to any cell from 3 different ways, from left, from top, from diagonal. So total no of paths to reach to that cell will be sum of all the paths to reach to left, top and diagonal cell. see picture

Example:

Complete Code:

Output:

No of Paths By Recursion: 13
No of paths By Dynamic Programming: 13

__________________________________________________ Top Companies Interview Questions..-

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________