Number of Unique Paths | Practice | GeeksforGeeks

Given a A X B matrix with your initial position at the top-left cell, find the number of possible unique paths to reach the bottom-right cell of the matrix from the initial position.
Note: Possible moves can be either down or right at any point in time, i.e., we can move to matrix[i+1][j] or matrix[i][j+1] from matrix[i][j].
Example 1:
Input:
A = 2, B = 2
Output: 2
Explanation: There are only two unique
paths to reach the end of the matrix of
size two from the starting cell of the
matrix.

Example 2:
Input:
A = 3, B = 4
Output: 10
Explanation: There are only 10 unique
paths to reach the end of the matrix of
size two from the starting cell of the
matrix.
Your Task:
Complete NumberOfPath() function which takes 2 arguments(A and B) and returns the number of unique paths from top-left to the bottom-right cell.
Expected Time Complexity: O(A*B).
Expected Auxiliary Space: O(A*B).
Constraints:
1 ≤ A ≤ 15
1 ≤ B ≤ 15
0
abro20 hours ago
class Solution{   private:   int n,m;   vector<vector<int>>dp;   public:   //Function to find total number of unique paths.      int solve( vector<vector<int>>&dp,int i,int j)   {       if(i==n or j==m)           return dp[i][j]=0;       if(dp[i][j]!=-1)           return dp[i][j];       if(i==n-1 and j==m-1)           return dp[i][j]=1;       return dp[i][j] = solve(dp,i+1,j)+ solve(dp,i,j+1);                     }            int NumberOfPath(int a, int b)   {       //code here       n=a;       m=b;       dp.resize(n+1, vector<int>(m+1,-1));       return solve(dp,0,0);   }};
+1
kashyapjhon2 days ago
C++ Solution Time=(0.01/1.31) EASY:
int NumberOfPath(int a, int b)   {       //code here       vector<vector<int> > dp(a+1,vector<int> (b+1,1));       for(int i=1;i<a;i++){           for(int j=1;j<b;j++){               dp[i][j]=dp[i-1][j]+dp[i][j-1];           }       }       return dp[a-1][b-1];   }
0
vinamrajha1 week ago
int NumberOfPath(int a, int b)
   {
       //code here
       int dp[a][b];
       for(int i =0; i<a; ++i){
           for(int j =0; j<b; ++j){
               if(i==0 || j==0)dp[i][j] =1;
               else dp[i][j] = dp[i-1][j] + dp[i][j-1];
           }
       }
       return dp[a-1][b-1];
   }
0
tirtha19025681 month ago
class Solution
{
   public static int NumberOfPath(int a, int b) 
   {
       int[][]dp = new int[a][b];
       
       for(int i=0;i<dp.length;i++){
           for(int j=0;j<dp[0].length;j++){
               dp[i][j] = -1;
           }
       }
       int ans = helper(dp,0,0,a,b);
       return ans;
   }
   
   static int helper(int[][]dp,int i,int j,int a,int b){
       if(i == a || j == b ) return 0;
       if(i == a-1 && j==b-1) return 1;
       
       if(dp[i][j] != -1) return dp[i][j];
       
       return dp[i][j] =  helper(dp,i+1,j,a,b) + helper(dp,i,j+1,a,b);
       
   }
}
0
vishalpandey100220002 months ago
public static int NumberOfPath(int a, int b)    {       //Your code here       int dp[][]=new int[a][b];       for (int i=0;i<a;i++){           for (int j=0;j<b;j++){               if (i==0 || j==0){                   dp[i][j]=1;               }               else{                   dp[i][j]=dp[i][j-1]+dp[i-1][j];               }           }       }       return dp[a-1][b-1];   }
0
ashiqhussainkumar1432 months ago
    //Function to find total number of unique paths.   int NumberOfPath(int N, int M)   {       //code here       int DP[N][M];       for(int i=0;i<N;i++){           for(int j=0;j<M;j++){               if(i==0 or j==0) DP[i][j]=1;               else DP[i][j]=DP[i][j-1] + DP[i-1][j];           }       }       return DP[N-1][M-1];   }
0
baibhavrajputt2 months ago
Java DP bottom up solution
class Solution
{
    //Function to find total number of unique paths.
    public static int NumberOfPath(int a, int b) 
    {
        //Your code here
        int dp[][] = new int[a][b];
        
        for(int i=0 ; i<a ; i++){
            for(int j=0 ; j<b ; j++){
                
                if(i==0 && j==0)
                    dp[i][j] = 1;
                
                else{
                    int up = 0;
                    int left = 0;
                    
                    if(i>0)
                        up = dp[i-1][j];
                    
                    if(j>0)
                        left = dp[i][j-1];
                        
                    dp[i][j] = up + left;
                }
            }
        }
        return dp[a-1][b-1];
    }
}
0
raunakmishra12432 months ago
int NumberOfPath(int n, int m)
    {
        int dp[n+1][m+1];
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(i==1 || j==1)
                dp[i][j]=1;
                
            }
        }
    
        for(int i=2;i<=n;i++)
        {
            for(int j=2;j<=m;j++)
            dp[i][j]=dp[i][j-1]+dp[i-1][j];
        }
        return dp[n][m];
    }
0
chaitanyasai3213 months ago
int NumberOfPath(int m, int n)   {    if(m == 1 || n == 1)           return 1;       m--;       n--;       if(m < n) {                        m = m + n;           n = m - n;           m = m - n;       }       long res = 1;       int j = 1;       for(int i = m+1; i <= m+n; i++, j++){                 res *= i;           res /= j;       }                  return (int)res;          }  
+2
devendray21063 months ago
C++ solution :-
    int NumberOfPath(int a, int b)
    {
        if(a==1 || b==1)
            return 1;
        
        return NumberOfPath(a-1,b)+NumberOfPath(a,b-1);
    }
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.