Watch 10 video solutions for Maximum Distance in Arrays, a medium level problem involving Array, Greedy. This walkthrough by NeetCodeIO has 13,337 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.
Example 1:
Input: arrays = [[1,2,3],[4,5],[1,2,3]] Output: 4 Explanation: 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:
Input: arrays = [[1],[1]] Output: 0
Constraints:
m == arrays.length2 <= m <= 1051 <= arrays[i].length <= 500-104 <= arrays[i][j] <= 104arrays[i] is sorted in ascending order.105 integers in all the arrays.Problem Overview: You are given multiple sorted arrays. Pick one element from one array and another element from a different array. The goal is to maximize the absolute difference |a - b|. Because each array is already sorted, the only candidates that matter are the first (minimum) and last (maximum) elements of each array.
Approach 1: Brute Force Comparison (O(n²) time, O(1) space)
The direct approach compares every pair of arrays. For each pair (i, j), compute two candidate distances: |arrays[i][0] - arrays[j][-1]| and |arrays[j][0] - arrays[i][-1]|. Since arrays are sorted, the extreme values produce the largest differences. Iterate through all pairs and track the maximum distance. This approach is simple and clearly shows the key observation about using only boundary elements, but the nested iteration results in O(n²) time where n is the number of arrays. It works fine for small inputs but does unnecessary comparisons.
Approach 2: Minimum and Maximum Tracking (Greedy) (O(n) time, O(1) space)
A more efficient solution keeps track of the smallest value and largest value seen so far while iterating through the arrays once. Start with the first array's minimum and maximum. For every subsequent array, compute two candidate distances: the difference between its maximum and the global minimum, and the difference between the global maximum and its minimum. Update the result with the larger of these two values. After processing the array, update the global minimum and maximum if needed. This works because the optimal pair must involve extremes across different arrays, and processing sequentially guarantees the values come from different arrays.
This technique is essentially a greedy strategy over arrays. Instead of comparing all combinations, you only maintain the best extremes encountered so far. Each array contributes at most two comparisons, which keeps the total work linear in the number of arrays.
Recommended for interviews: The greedy minimum/maximum tracking solution is the expected answer. Interviewers want to see that you recognize the sorted property and reduce the problem to comparing boundary values. Mentioning the brute force approach first shows understanding of the search space, but implementing the greedy linear pass demonstrates optimization skills and awareness of time complexity tradeoffs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Comparison | O(n²) | O(1) | Useful for understanding the problem or when the number of arrays is very small |
| Minimum and Maximum Tracking (Greedy) | O(n) | O(1) | Best approach for interviews and production code; processes arrays in a single pass |