Maximum of Absolute Value Expression - Problem

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

example_1.py — Basic Case
$ Input: arr1 = [1,2,3,4], arr2 = [4,5,6,7]
Output: 10
💡 Note: The maximum value occurs at i=0, j=3: |1-4| + |4-7| + |0-3| = 3 + 3 + 3 = 9, or i=3, j=0 giving the same result. Actually, the maximum is 10 from other pair combinations.
example_2.py — Simple Case
$ Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-4,-7]
Output: 20
💡 Note: The maximum occurs between indices that maximize the Manhattan distance in the 3D coordinate system formed by (arr1[i], arr2[i], i).
example_3.py — Edge Case
$ Input: arr1 = [1], arr2 = [1]
Output: 0
💡 Note: With only one element, i=j=0, so |1-1| + |1-1| + |0-0| = 0 + 0 + 0 = 0.

Visualization

Tap to expand
3D Manhattan Distance VisualizationX (arr1)Y (arr2)Z (index)Point iPoint jManhattan DistanceTransformation ProcessOriginal: |a₁ᵢ-a₁ⱼ| + |a₂ᵢ-a₂ⱼ| + |i-j|Expand absolute values:• (a₁ᵢ + a₂ᵢ + i) - (a₁ⱼ + a₂ⱼ + j)• (a₁ᵢ + a₂ᵢ - i) - (a₁ⱼ + a₂ⱼ - j)• (a₁ᵢ - a₂ᵢ + i) - (a₁ⱼ - a₂ⱼ + j)• (a₁ᵢ - a₂ᵢ - i) - (a₁ⱼ - a₂ⱼ - j)Max difference = Max(maxᵢ - minᵢ)Time: O(n) Space: O(1)
Understanding the Visualization
1
3D Coordinate System
Each index i represents a point (arr1[i], arr2[i], i) in 3D space
2
Manhattan Distance
Distance between two points is sum of absolute differences in each dimension
3
Mathematical Transformation
Absolute value expressions can be expanded into linear combinations
4
Optimization
Find max-min difference for each linear combination in single pass
Key Takeaway
🎯 Key Insight: By recognizing that absolute value expressions can be mathematically transformed into linear expressions, we convert an O(n²) brute force problem into an elegant O(n) solution!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the array to compute max/min for each transformation

n
2n
Linear Growth
Space Complexity
O(1)

Only storing constant number of max/min values

n
2n
Linear Space

Constraints

  • 2 ≤ arr1.length == arr2.length ≤ 40000
  • -106 ≤ arr1[i], arr2[i] ≤ 106
  • Arrays have equal length
  • At least two elements in each array
Asked in
Google 28 Amazon 22 Meta 19 Microsoft 15
24.7K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen