{x}
blog image

Maximum of Absolute Value Expression

Given two arrays of integers 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.

 

Example 1:

Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
Output: 13

Example 2:

Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
Output: 20

 

Constraints:

  • 2 <= arr1.length == arr2.length <= 40000
  • -10^6 <= arr1[i], arr2[i] <= 10^6

Solution Explanation for LeetCode 1131: Maximum of Absolute Value Expression

This problem asks to find the maximum value of the expression |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j| for all possible pairs of indices i and j in the input arrays arr1 and arr2. A brute-force approach would have O(n^2) time complexity, which is inefficient for large input arrays. The optimal solution leverages mathematical properties to reduce the time complexity to O(n).

Mathematical Optimization:

The key insight lies in simplifying the absolute value expression. We can rewrite it by considering four cases based on the signs of arr1[i] - arr1[j] and arr2[i] - arr2[j]. However, a more concise approach involves recognizing that the expression can be reformulated as the maximum difference between two values of the form a * arr1[i] + b * arr2[i] + i, where a and b are either 1 or -1.

Specifically, the expression can be rewritten as the maximum difference between any two values among the following four expressions, for all pairs (i, j):

  1. arr1[i] + arr2[i] + i
  2. arr1[i] - arr2[i] + i
  3. -arr1[i] + arr2[i] + i
  4. -arr1[i] - arr2[i] + i

Algorithm:

  1. Iterate through coefficient combinations: We loop through the four combinations of a and b from {-1, 1}.

  2. Calculate values: For each combination, we iterate through the arrays, calculating a * arr1[i] + b * arr2[i] + i for each index i.

  3. Track maximum and minimum: For each combination of a and b, we keep track of the maximum (mx) and minimum (mi) values encountered so far.

  4. Update maximum difference: After processing all indices for a given combination, we update the overall maximum difference (ans) with max(ans, mx - mi).

  5. Return maximum difference: Finally, we return ans, which represents the maximum value of the original absolute value expression.

Time and Space Complexity:

  • Time Complexity: O(n), where n is the length of the input arrays. This is because we iterate through the arrays a constant number of times (four times, to be precise).

  • Space Complexity: O(1). We use only a constant amount of extra space to store variables.

Code Implementation (Python):

from itertools import product
 
class Solution:
    def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int:
        ans = 0
        for a, b in product([-1, 1], repeat=2):
            values = [a * x + b * y + i for i, (x, y) in enumerate(zip(arr1, arr2))]
            ans = max(ans, max(values) - min(values))
        return ans
 

Code Implementation (Java):

class Solution {
    public int maxAbsValExpr(int[] arr1, int[] arr2) {
        int ans = 0;
        for (int a = -1; a <= 1; a += 2) {
            for (int b = -1; b <= 1; b += 2) {
                int minVal = Integer.MAX_VALUE;
                int maxVal = Integer.MIN_VALUE;
                for (int i = 0; i < arr1.length; i++) {
                    int val = a * arr1[i] + b * arr2[i] + i;
                    minVal = Math.min(minVal, val);
                    maxVal = Math.max(maxVal, val);
                }
                ans = Math.max(ans, maxVal - minVal);
            }
        }
        return ans;
    }
}

The other languages (C++, Go, TypeScript) would follow a very similar structure to the Python and Java examples, implementing the same algorithm with minor syntax differences. The core idea of efficiently calculating the maximum difference remains the same across all implementations.