Dsa
Matrix
Hard
Slope of Matrix Lines
You have a 2D matrix representing the heights of a terrain. The terrain is divided into a grid and you need to find the maximum slope between any two adjacent cells in the matrix. The slope is defined as the absolute difference in heights between two neighboring cells divided by the distance between them, which is 1 for adjacent cells (up, down, left, right).
Input:
- The first line contains two integers m (rows) and n (columns),
- The next m lines contain n integers representing the heights of terrain.
Output:
- A single float representing the maximum slope rounded to 2 decimal places.
Example:
Input:
2 2
1 3
2 4
Output:
2.00
Key concepts
matrixgeometryanalysis
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now