Maximum Distance in Arrays - Problem
You are given m arrays, 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|.
Return the maximum distance.
Input & Output
Example 1 — Basic Case
$
Input:
arrays = [[1,2,3],[4,5],[1,2,3]]
›
Output:
4
💡 Note:
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
Example 2 — Different Arrays
$
Input:
arrays = [[1],[1]]
›
Output:
0
💡 Note:
Both arrays contain only 1, so the maximum distance is |1-1| = 0.
Example 3 — Negative Numbers
$
Input:
arrays = [[-1,1],[-3,1,4],[-2,-1,0,2]]
›
Output:
7
💡 Note:
Maximum distance is achieved by picking -3 from second array and 4 from second array... wait, they must be from different arrays. Pick -3 from second array and 4 from third array gives |4-(-3)| = 7.
Constraints
- m == arrays.length
- 2 ≤ m ≤ 105
- 1 ≤ arrays[i].length ≤ 500
- -104 ≤ arrays[i][j] ≤ 104
- arrays[i] is sorted in ascending order.
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
Multiple sorted arrays with different ranges
2
Find Extremes
Identify minimum and maximum values across arrays
3
Calculate Distance
Maximum distance is between global min and max from different arrays
Key Takeaway
🎯 Key Insight: Maximum distance always comes from pairing extreme values from different arrays
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code