You are present at point ‘A’ which is the top-left cell of an M X N matrix, your destination is point ‘B’, which is the bottom-right cell of the same matrix. Your task is to find the total number of unique paths from point ‘A’ to point ‘B’.In other words, you will be given the dimensions of the matrix as integers ‘M’ and ‘N’, your task is to find the total number of unique paths from the cell MATRIX[0][0] to MATRIX['M' - 1]['N' - 1].
To traverse in the matrix, you can either move Right or Down at each step. For example in a given point MATRIX[i] [j], you can move to either MATRIX[i + 1][j] or MATRIX[i][j + 1].
Input Format:
The first line of input contains an integer 'T' representing the number of the test case.
The first and the only line of each test case contains two space-separated integers ‘M’ and ‘N’, denoting the number of rows and number of columns of the matrix respectively.
Output Format:
For every test case, return a single integer, which is the total number of unique paths for traveling from top-left to bottom-right cells of the matrix.
The output of each test case is printed in a separate line.
Note:
You don’t have to print anything, it has already been taken care of. Just implement the given function.
Constraints:
1 ≤ T ≤ 100
1 ≤ M ≤ 15
1 ≤ N ≤ 15
Where ‘M’ is the number of rows and ‘N’ is the number of columns in the matrix.
Time limit: 1 sec
Step 1 first create a array/list variable of size same as given size of m x n grid and assign 1st ROW & COL to 1.
Step 2 Create a loop for row (starting from 1 to m) and then Create a loop for col (sta...read more
We can easily count the total number of paths by making a recursive algorithm.
The steps are as follows:
- We are given a function UNIQUEPATHS(), which takes two integers ‘M’ and ‘N’ a...read more
Our last approach was very simple and easy, but it’s time complexity was of exponential order. We can improve our solution by taking care of the overlapping subproblems. Thus, in this appr...read more
As we already know that we can improve our solution by taking care of the overlapping subproblems. Also, we can obtain the optimal solution of the problem by using the optimal solut...read more
The previous approach improved our solution to a large extent by avoiding the common subproblem recomputations. But the space complexity can be even further improved...read more
Top Amazon Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in Amazon Software Developer
Reviews
Interviews
Salaries
Users/Month