Print All Possible Paths From Top Left Corner To Bottom Right Corner Of A 2-D Matrix

You are given an ‘M*N’ Matrix, You need to print all possible paths from its top left corner to the bottom right corner if given that you can either move right i.e from (i,j) to (i,j+1) or down i.e from (i, j) to (i+1, j).

For Example :
1 2 3
4 5 6

For the above 2*3 matrix , possible paths are (1 2 3 6) , (1 2 5 6) , (1 4 5 6).
Note :
You can return the paths in any order.
Input Format :
The first line contains two space-separated integers, ‘M’ and ‘N’ denoting the number of rows and columns of the given matrix.

Each of the following ‘M’ lines contains N space-separated numbers.
Output Format :
Print all the paths in a new line.
Note :
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints :
1 <= M, N <= 10

Time Limit: 1 sec
CodingNinjas
author
2y
Recursion

The basic idea to solve this problem is to use recursion. Recursively call function for next row ( row+1,col ) and next column ( row, col+1 ). If the row is equal to M-1 or the column is equa...read more

Help your peers!
Add answer anonymously...
JPMorgan Chase & Co. Machine Learning Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter