You are given marrays, where each array is sorted in ascending order.
You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a - b|.
Input: arrays =[[1,2,3],[4,5],[1,2,3]]Output: 4Explanation: One way to reach the maximum distance 4is to pick 1in the first or third array and pick 5in the second array.
The distance between two integers (a) and (b) from two different arrays is (|a - b|).
Leverage Sorted Property:
Since each array is sorted in ascending order, the smallest number from an array is the first element, and the largest number is the last element.
Track Min and Max Values across Arrays:
As we iterate through each array, we keep track of the global minimum value and the global maximum value so far.
We also compute and update the maximum distance by comparing the first element of the current array with the global maximum value encountered so far and the last element of the current array with the global minimum value encountered so far.
Update the global minimum and global maximum values accordingly.
⏰ Time complexity: O(m) where m is number of arrays, as we pass through each array exactly once, that too checking only min and max values.
🧺 Space complexity: O(1)
Method 2 - Calculating Global and Second maxima and minima#
We can use a slightly different approach by finding the global minimum and maximum values among different arrays and handle the cases when the minimum and maximum values come from the same array by comparing with the second minimum and second maximum values from other arrays. Here’s the detailed approach:
publicclassSolution {
publicintmaxDistance(List<List<Integer>> arrays) {
// Variables to track global min and max and their indicesint globalMin = Integer.MAX_VALUE;
int minIndex =-1;
int globalMax = Integer.MIN_VALUE;
int maxIndex =-1;
// First pass to find global min and maxfor (int i = 0; i < arrays.size(); i++) {
int currentMin = arrays.get(i).get(0);
int currentMax = arrays.get(i).get(arrays.get(i).size() - 1);
if (currentMin < globalMin) {
globalMin = currentMin;
minIndex = i;
}
if (currentMax > globalMax) {
globalMax = currentMax;
maxIndex = i;
}
}
// If global min and max are from different arrays, return the distanceif (minIndex != maxIndex) {
return globalMax - globalMin;
}
// Variables to track the second min and maxint secondMin = Integer.MAX_VALUE;
int secondMax = Integer.MIN_VALUE;
// Second pass to find second min and max for the same-index casefor (int i = 0; i < arrays.size(); i++) {
if (i != minIndex) {
int currentMin = arrays.get(i).get(0);
int currentMax = arrays.get(i).get(arrays.get(i).size() - 1);
if (currentMin < secondMin) {
secondMin = currentMin;
}
if (currentMax > secondMax) {
secondMax = currentMax;
}
}
}
// Calculate possible maximum distancesint maxDist1 = globalMax - secondMin;
int maxDist2 = secondMax - globalMin;
// Return the maximum of the two computed distancesreturn Math.max(maxDist1, maxDist2);
}
}