You're given two integer arrays arr1 and arr2 of equal length. Your task is to find the maximum value of the expression:
|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|
where the maximum is taken over all possible pairs of indices 0 ≤ i, j < arr1.length.
This expression represents the Manhattan distance between two points in 3D space: (arr1[i], arr2[i], i) and (arr1[j], arr2[j], j). You need to find the pair of points that are furthest apart according to this metric.
Example: If arr1 = [1, 2, 3] and arr2 = [4, 5, 6], one possible calculation would be for i=0, j=2: |1-3| + |4-6| + |0-2| = 2 + 2 + 2 = 6
Input & Output
Visualization
Time & Space Complexity
Single pass through the array to compute max/min for each transformation
Only storing constant number of max/min values
Constraints
- 2 ≤ arr1.length == arr2.length ≤ 40000
- -106 ≤ arr1[i], arr2[i] ≤ 106
- Arrays have equal length
- At least two elements in each array