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.

AnswerBot
4mo

Find the maximum difference between elements in a matrix with specific conditions.

  • Iterate through all possible pairs of indices (a, b) and (c, d) where c > a and d > b.

  • Calculate the difference between...read more

Help your peers!
Select
Add answer anonymously...

Top Software Developer Intern Interview Questions Asked at Hike

Q. Infix to Postfix Conversion Convert a given infix expression, represented as a s...read more
Q. Maximum Size Rectangle Sub-matrix with All 1's Problem Statement You are provide...read more
Q. Binary Tree Maximum Path Sum Problem Statement Determine the maximum path sum fo...read more
Software Developer Intern 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