Longest Increasing Path in Matrix Problem Statement

Given a 2-D matrix mat with 'N' rows and 'M' columns, where each element at position (i, j) is mat[i][j], determine the length of the longest increasing path starting from mat[0][0] and ending at any cell.

Explanation:

You can move:

  • To mat[i+1][j] if mat[i+1][j] > mat[i][j]
  • To mat[i][j+1] if mat[i][j+1] > mat[i][j]

Your task is to compute the longest path possible starting at (0,0).

Input:

The first line contains an integer 'T' representing the number of test cases. Each test case contains: 
The first line with two space-separated integers, 'N' and 'M'.
The next 'N' lines contain 'M' space-separated integers each, describing the matrix 'mat'.

Output:

For each test case, output one integer per line representing the length of the longest increasing path for the given matrix.

Example:

Input:
2 
2 2
1 2
3 4
3 3
1 2 3
6 5 4
7 8 9
Output:
3 
5
Explanation:

In the first test case, the longest path is 1 -> 2 -> 4 with length 3. In the second test case, the longest path is 1 -> 2 -> 3 -> 4 -> 9 with length 5.

Constraints:

  • 1 <= T <= 50
  • 1 <= N <= 100
  • 1 <= M <= 100
  • -109 <= mat[i][j] <= 109

Note: Implement the function to calculate the path; no need to handle input/output as it's managed externally.

Be the first one to answer
Add answer anonymously...
JPMorgan Chase & Co. SDE-2 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

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