Maximum Difference in Matrix

Given an n x n matrix mat[n][n] of integers, find the maximum value of mat[c][d] - mat[a][b] for all possible indices where c > a and d > b.

Input:

The first line contains a single integer T representing the number of test cases. For each test case:
The first line contains a single integer N denoting the size of the matrix.
The next N lines contain 'N' integers each, where each line represents a row of the matrix.

Output:

For each test case, output a single integer indicating the maximum difference calculated for that test case.
Each result should be printed on a new line.

Example:

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

In this matrix, the maximum difference is 8 (from mat[2][2] - mat[0][0]).

Constraints:

  • 1 ≤ N ≤ 100
  • 1 ≤ mat[i][j] ≤ 108
Note:

No need to print in your solution, just implement the function.

Be the first one to answer
Add answer anonymously...
Hike Software Developer Intern 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