Maximum of Absolute Value Expression - Problem

Given two arrays of integers arr1 and arr2 with equal lengths, return the maximum value of:

|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|

where the maximum is taken over all 0 <= i, j < arr1.length.

Input & Output

Example 1 — Basic Case
$ Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
Output: 13
💡 Note: Maximum occurs at i=3, j=0: |4-1| + |6-(-1)| + |3-0| = 3 + 7 + 3 = 13
Example 2 — Small Array
$ Input: arr1 = [1,-2], arr2 = [-1,4]
Output: 9
💡 Note: Only two elements: |1-(-2)| + |-1-4| + |0-1| = 3 + 5 + 1 = 9
Example 3 — Same Values
$ Input: arr1 = [1,1,1], arr2 = [2,2,2]
Output: 2
💡 Note: Arrays have same values, maximum comes from index difference: |1-1| + |2-2| + |0-2| = 0 + 0 + 2 = 2

Constraints

  • 2 ≤ arr1.length == arr2.length ≤ 40000
  • -106 ≤ arr1[i], arr2[i] ≤ 106

Visualization

Tap to expand
Maximum of Absolute Value Expressionarr1:1234arr2:-1456Expression:|arr1[i] - arr1[j]|+ |arr2[i] - arr2[j]|+ |i - j|Check pair i=3, j=0:|4-1| + |6-(-1)| + |3-0| = 3 + 7 + 3Maximum: 13
Understanding the Visualization
1
Input
Two arrays of equal length
2
Expression
Calculate |arr1[i]-arr1[j]| + |arr2[i]-arr2[j]| + |i-j|
3
Maximum
Find the maximum value over all pairs
Key Takeaway
🎯 Key Insight: Transform absolute value expressions into linear cases to avoid checking all pairs
Asked in
Google 25 Facebook 18 Amazon 12
28.0K Views
Medium Frequency
~25 min Avg. Time
890 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