Magicsheet logo

Maximum of Absolute Value Expression

Medium
12.5%
Updated 8/1/2025

Asked by 3 Companies

Topics

Maximum of Absolute Value Expression

What is this problem about?

The Maximum of Absolute Value Expression coding problem asks you to find the maximum value of the expression |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j| for all valid indices i and j (where i != j). You are given two arrays arr1 and arr2 of equal length.

Why is this asked in interviews?

Microsoft, Amazon, and Google use this problem to test a candidate's mathematical insight and ability to simplify expressions involving absolute values. The key is to realize that |X - Y| can be broken down into (X - Y) or (Y - X), leading to a finite number of combinations to check, or a transformation that eliminates absolute values.

Algorithmic pattern used

Expression Transformation / Case Analysis: The absolute value function |x - y| can be rewritten as max(x - y, y - x). When you have multiple absolute value terms, the expression |A| + |B| + |C| can be expanded by considering the four possible combinations of signs for the terms (arr1[i] - arr1[j]), (arr2[i] - arr2[j]), and (i - j). However, directly expanding is 2^3 = 8 forms, which simplifies to 4 distinct forms when grouped.

Specifically, the expression |X_i - X_j| + |Y_i - Y_j| + |Z_i - Z_j| can be rewritten as max_{s1, s2, s3 \in {-1, 1}} ( (s1 * X_i + s2 * Y_i + s3 * Z_i) - (s1 * X_j + s2 * Y_j + s3 * Z_j) ). In our case, X_k = arr1[k], Y_k = arr2[k], Z_k = k. So we need to maximize (arr1[i] \pm arr2[i] \pm i) - (arr1[j] \pm arr2[j] \pm j). For each fixed combination of signs (s1, s2, s3), let val_k = s1*arr1[k] + s2*arr2[k] + s3*k. Then we need to maximize val_i - val_j. The maximum of val_i - val_j over all i, j is simply max(val_k) - min(val_k) for that specific (s1, s2, s3) combination.

There are 8 combinations of (s1, s2, s3) but due to symmetry (A_i - A_j) and -(A_j - A_i), we only need to consider 4 unique combinations:

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

Algorithm:

  1. Initialize max_expression_value = 0.
  2. Define the four coefficient tuples: (1, 1, 1), (1, 1, -1), (1, -1, 1), (1, -1, -1).
  3. For each (s1, s2, s3) tuple: a. Initialize current_max_val = -infinity and current_min_val = +infinity. b. Iterate k from 0 to n-1: i. Calculate val_k = s1 * arr1[k] + s2 * arr2[k] + s3 * k. ii. Update current_max_val = max(current_max_val, val_k). iii. Update current_min_val = min(current_min_val, val_k). c. Update max_expression_value = max(max_expression_value, current_max_val - current_min_val).
  4. Return max_expression_value.

Time Complexity: O(N) because we iterate through the arrays 4 times (constant number of passes).

Example explanation

arr1 = [1, 2, 3], arr2 = [1, 2, 3]. Let N=3. Coefficients: (s1, s2, s3)

  1. (1, 1, 1): val_k = arr1[k] + arr2[k] + k

    • val_0 = 1 + 1 + 0 = 2
    • val_1 = 2 + 2 + 1 = 5
    • val_2 = 3 + 3 + 2 = 8 max(val_k) - min(val_k) = 8 - 2 = 6. max_expression_value = 6.
  2. (1, 1, -1): val_k = arr1[k] + arr2[k] - k

    • val_0 = 1 + 1 - 0 = 2
    • val_1 = 2 + 2 - 1 = 3
    • val_2 = 3 + 3 - 2 = 4 max(val_k) - min(val_k) = 4 - 2 = 2. max_expression_value = max(6, 2) = 6.
  3. (1, -1, 1): val_k = arr1[k] - arr2[k] + k

    • val_0 = 1 - 1 + 0 = 0
    • val_1 = 2 - 2 + 1 = 1
    • val_2 = 3 - 3 + 2 = 2 max(val_k) - min(val_k) = 2 - 0 = 2. max_expression_value = max(6, 2) = 6.
  4. (1, -1, -1): val_k = arr1[k] - arr2[k] - k

    • val_0 = 1 - 1 - 0 = 0
    • val_1 = 2 - 2 - 1 = -1
    • val_2 = 3 - 3 - 2 = -2 max(val_k) - min(val_k) = 0 - (-2) = 2. max_expression_value = max(6, 2) = 6.

Result: 6.

Common mistakes candidates make

  • Brute-force O(N^2) solution: Iterating all pairs (i, j) and calculating the expression for each. This will TLE for large N.
  • Incorrectly expanding absolute values: Missing cases or making algebraic errors. The fixed set of 4 linear combinations covers all possibilities.
  • Off-by-one or index errors: While max(V[i]) - min(V[k]) doesn't strictly enforce i != k, the i=k case results in 0, which won't be the maximum unless all values are the same. This method correctly finds the maximum difference between any two elements.

Interview preparation tip

For the Array Math interview pattern, problems involving maximizing/minimizing absolute value expressions often reduce to a fixed number of linear combinations. The trick is to systematically remove the absolute values by considering all sign possibilities.

Similar Questions