624. Maximum Distance in Arrays
MediumSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
The problem asks for the maximum absolute difference between two integers picked from two different arrays. We are given m arrays, each sorted in ascending order.
Solution
Maximum Distance in Arrays in Python
class Solution:
def maxDistance(self, arrays: List[List[int]]) -> int:
max_dist = 0
# Initialize min_val and max_val with the first array's bounds.
min_val = arrays[0][0]
max_val = arrays[0][-1]
# Iterate through the rest of the arrays, starting from the second one.
for i in range(1, len(arrays)):
current_min = arrays[i][0]
current_max = arrays[i][-1]
# Calculate potential max distance with the current array and the
# min/max values seen in previous arrays. This ensures we are picking
# from two different arrays.
# Case 1: Pick max from current array, min from the pool of previous arrays.
max_dist = max(max_dist, current_max - min_val)
# Case 2: Pick min from current array, max from the pool of previous arrays.
max_dist = max(max_dist, max_val - current_min)
# Update the overall min and max values seen so far to include the current array.
min_val = min(min_val, current_min)
max_val = max(max_val, current_max)
return max_dist
Maximum Distance in Arrays in Java
class Solution {
public int maxDistance(List<List<Integer>> arrays) {
int maxDist = 0;
// Initialize minVal and maxVal with the first array's bounds.
int minVal = arrays.get(0).get(0);
int maxVal = arrays.get(0).get(arrays.get(0).size() - 1);
// Iterate through the rest of the arrays, starting from the second one.
for (int i = 1; i < arrays.size(); i++) {
List<Integer> currentArray = arrays.get(i);
int currentMin = currentArray.get(0);
int currentMax = currentArray.get(currentArray.size() - 1);
// Calculate potential max distance with the current array and the
// min/max values seen in previous arrays. This ensures we are picking
// from two different arrays.
// Case 1: Pick max from current array, min from the pool of previous arrays.
maxDist = Math.max(maxDist, currentMax - minVal);
// Case 2: Pick min from current array, max from the pool of previous arrays.
maxDist = Math.max(maxDist, maxVal - currentMin);
// Update overall min and max for next iterations to include the current array.
minVal = Math.min(minVal, currentMin);
maxVal = Math.max(maxVal, currentMax);
}
return maxDist;
}
}
Maximum Distance in Arrays in C++
class Solution {
public:
int maxDistance(vector<vector<int>>& arrays) {
int max_dist = 0;
// Initialize min_val and max_val with the first array's bounds.
int min_val = arrays[0].front();
int max_val = arrays[0].back();
// Iterate through the rest of the arrays, starting from the second one.
for (int i = 1; i < arrays.size(); ++i) {
// Get the current array's minimum and maximum values.
int current_min = arrays[i].front();
int current_max = arrays[i].back();
// Calculate potential max distance with the current array and the
// min/max values seen in previous arrays. This ensures we are picking
// from two different arrays.
// Case 1: Pick max from current array, min from the pool of previous arrays.
max_dist = max(max_dist, current_max - min_val);
// Case 2: Pick min from current array, max from the pool of previous arrays.
max_dist = max(max_dist, max_val - current_min);
// Update the overall min and max values seen so far to include the current array.
min_val = min(min_val, current_min);
max_val = max(max_val, current_max);
}
return max_dist;
}
};
Maximum Distance in Arrays in TypeScript
function maxDistance(arrays: number[][]): number {
let maxDist = 0;
// Initialize minVal and maxVal with the first array's bounds.
let minVal = arrays[0][0];
let maxVal = arrays[0][arrays[0].length - 1];
// Iterate through the rest of the arrays, starting from the second one.
for (let i = 1; i < arrays.length; i++) {
const currentArray = arrays[i];
const currentMin = currentArray[0];
const currentMax = currentArray[currentArray.length - 1];
// Calculate potential max distance with the current array and the
// min/max values seen in previous arrays. This ensures we are picking
// from two different arrays.
// Case 1: Pick max from current array, min from the pool of previous arrays.
maxDist = Math.max(maxDist, currentMax - minVal);
// Case 2: Pick min from current array, max from the pool of previous arrays.
maxDist = Math.max(maxDist, maxVal - currentMin);
// Update overall min and max for next iterations to include the current array.
minVal = Math.min(minVal, currentMin);
maxVal = Math.max(maxVal, currentMax);
}
return maxDist;
};
More Medium LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.