Longest Increasing Path In A 2D Matrix

You have been given a MATRIX of non-negative integers of size N x M where 'N' and 'M' denote the number of rows and columns, respectively.

Your task is to find the length of the longest increasing path when you can move to either four directions: left, right, up or down from each cell. Moving diagonally or outside the boundary is not allowed.

Note: A sequence of integers is said to form an increasing path in the matrix if the integers when traversed along the allowed directions can be arranged in strictly increasing order. The length of an increasing path is the number of integers in that path.

For example :

3 2 2
5 6 6
9 5 11 

In the given matrix, 3 →  5 →  6 and 3 →  5 →  9 form increasing paths of length 3 each.
Input Format :
The first line of input contains an integer 'T' representing the number of test cases or queries to be processed. Then the test case follows.

The first line of each test case contains two space-separated integers 'N', 'M' where 'N' and 'M' denote the number of rows and columns of the matrix, respectively.

From the second line of each test case, the next 'N' lines represent the rows of the MATRIX. Every row contains 'M' single space-separated integers.
Output Format :
For each test case, print the length of the longest increasing path.

Print the output of each test case in a separate line.

Note :
You do not need to print anything; it has already been taken care of. Just implement the function.
Constraints :
1 <= T <= 100
1 <= N <= 50
1 <= M <= 50
0 <= MATRIX[i] <= 10^5

Time Limit: 1sec
AnswerBot
1y

The task is to find the length of the longest increasing path in a 2D matrix, where you can move in four directions: left, right, up, or down from each cell.

  • Traverse the matrix and for each cell, find...read more

CodingNinjas
author
2y

This was one of the most interesting questions I faced in all my campus interviews . I was initially thinking of using recursion and then optimising it through memoisation but there was a risk of gett...read more

CodingNinjas
author
2y
DFS

The idea is to use DFS to find the length of the increasing path for every element.

We traverse the matrix and find the length of the longest increasing path starting from each cell.

  1. For each ele...read more
CodingNinjas
author
2y
DFS with memoization

In the previous approach, the longest path starting from each cell is calculated again every time that cell is revisited. So, the idea is to use DFS with memoization to improve tim...read more

Add answer anonymously...
Facebook Software Developer 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