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.

AnswerBot
4mo

The problem involves finding the length of the longest increasing path in a 2-D matrix starting from a given cell.

  • Use dynamic programming to keep track of the longest increasing path starting from eac...read more

Help your peers!
Select
Add answer anonymously...

JPMorgan Chase & Co. SDE-2 interview questions & answers

A SDE-2 was asked 10mo agoQ. Design a parking system for a parking lot. The parking lot has three kinds of pa...read more
A SDE-2 was asked 10mo agoQ. Given a string s, find the number of substrings without repeating characters.
A SDE-2 was asked 10mo agoQ. Debugging a Java Code

Popular interview questions of SDE-2

A SDE-2 was asked 10mo agoQ1. Design a parking system for a parking lot. The parking lot has three kinds of pa...read more
A SDE-2 was asked 10mo agoQ2. Given a string s, find the number of substrings without repeating characters.
A SDE-2 was asked 10mo agoQ3. Debugging a Java Code
JPMorgan Chase & Co. SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits