Number of paths | Practice | GeeksforGeeks

The problem is to count all the possible paths from top left to bottom right of a MxN matrix with the constraints that from each cell you can either move to right or down.
Example 1:
Input:
M = 3 and N = 3
Output: 6
Explanation:
Let the given input 3*3 matrix is filled 
as such:
A B C
D E F
G H I
The possible paths which exists to reach 
'I' from 'A' following above conditions 
are as follows:ABCFI, ABEHI, ADGHI, ADEFI, 
ADEHI, ABEFI

 
Example 2:
Input:
M = 2 and N = 8
Output: 8


Your Task:  
You don't need to read input or print anything. Your task is to complete the function numberOfPaths() which takes the integer M and integer N as input parameters and returns the number of paths..

Expected Time Complexity: O(m + n - 1))
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ M, N ≤ 10
0
sangamme1113 weeks ago
if(n==1 || m==1){
return 1;}
return numberOfPaths(m-1,n) + numberOfPaths(m,n-1);
easy method
0
mohdraza3 weeks ago
class Solution{      long numberOfPaths(int m, int n) {       return fact(m+n-2)/ (fact(n-1)* fact(m-1)); //numberOfPaths(m - 1, n) + numberOfPaths(m, n - 1);   }   long fact(int n)   {       if (n == 0)         return 1;                return n*fact(n-1);   }   }
+2
aakasshuit2 months ago
//Java Solution

     if (m == 1 || n == 1)
            return 1;
     return numberOfPaths(m - 1, n) + numberOfPaths(m, n - 1);
0
saurabhsharma295202 months ago
if(i == m || j == n) return 1;   return numberOfPaths(m,n,i+1,j)+numberOfPaths(m,n,i,j+1);
0
pardeep12 months ago
long long  numberOfPaths(int m, int n){   int t[m];    memset(t, 0, sizeof(t));   t[0]=1;   for(int i=0;i<n;i++)   {       for(int j=1;j<m;j++)       {           t[j]=t[j]+t[j-1];           //cout<<t[j]<<" ";       }       //cout<<endl;   }   return t[m-1];}
+1
chechipresh2 months ago
Total steps to traverse=m+n-2 
Steps in downward direction=m-1 
Steps in right direction=n-1 
Then,Total no. of ways=(m+n-2)!/(m-1)!(n-1)!
long long  numberOfPaths(int m, int n){   // Code Here    long long ft=1,fm=1,fn=1;   int i;   for (i=1;i<=m+n-2;i++){       if (i<=m-1)          fm*=i;       if (i<=n-1)          fn*=i;       ft*=i;   }   return ft/(fm*fn);
}
+1
cs21m0592 months ago
 long long dp[m][n];   memset(dp,0,sizeof(dp));      for(int  i = 0 ;  i < m ; i ++)   {       for(int  j = 0 ; j < n ; j ++)       {           if(i == 0 && j == 0) dp[i][j] = 1;           else if( i == 0)           {               dp[i][j] = 1 ;           }           else if(j == 0)           {               dp[i][j] = 1 ;           }           else           {               dp[i][j] = dp[i][j-1] + dp[i-1][j];           }       }   }         return dp[m-1][n-1];
0
huzefabohra5262 months ago
 
Solution
 Approach is iterative .
Basically based on combinatorics....
in order to cover (m+n) distance of which m right moves and n down moves can be done in (m+n)!/(m!*n!)
 
Now challenge is to do in O(m+n-1) time complexity which can be done by cancelling (m-1)! or (n-1)! term from  numertor.
Interestingly i have checked bigger of m & n and cancelled bigger term so that loop iterates for less number .
 
 
long long  numberOfPaths(int m, int n){      double result=1;   if(m==1 || n==1)   return 1;      else{    if(m<n)
       {    for(int j=1;j<=(m-1);j++)   result = result*(n-1+j)/j;                 // (n-1)! is cancelled  as n is greater
       }   else      {    for(int j=1;j<=(n-1);j++)   result = result*(m-1+j)/j;             // (m-1)! is cancelled  as m is greater
      }         
 }             }
   return int(result);}
0
hamidnourashraf2 months ago
def rec_path(self, n, m, i, j):
       if i == m -1 and j == n-1:
           return 1
       if i > m-1 or j > n-1: 
           return 0
       return self.rec_path(n,m, i+1, j) + self.rec_path(n,m, i, j+1)  
 
 
    def numberOfPaths (self, n, m):
       res = [[-1 for x in range(n+1)] for y in range(m+1)]
       res[m-1][n-1] = 1
       for i in range(m-1, -1, -1):
           for j in range(n-1, -1, -1):
               if res[i][j] == -1:
                   if res[i][j+1] == -1:
                       res[i][j+1] = self.rec_path(n, m, i, j+1)
                   if res[i+1][j] == -1:
                       res[i+1][j] = self.rec_path(n, m, i+1, j)
                   res[i][j] = res[i+1][j] + res[i][j+1]
       return res[0][0]
0
hamidnourashraf2 months ago
def rec_path(self, n, m, i, j):
        if i == m -1 and j == n-1:
            return 1
        if i > m-1 or j > n-1: 
            return 0
        return self.rec_path(n,m, i+1, j) + self.rec_path(n,m, i, j+1)
def numberOfPaths (self, n, m):  ## recursive
       return self.rec_path(n, m, 0, 0)
            
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.