Maximum of Absolute Value Expression
Maximum of Absolute Value Expression is a medium arrays & hashing problem solved with the math / number theory pattern.
The best approach, optimal (expand the absolute values into 4 sign cases), runs in O(n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from brute force (every pair) up.
Problem
You are given two integer arrays arr1 and arr2 of the same length n.
Over all pairs of indices 0 <= i, j < n, return the largest value of
|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|.
Examples
Example 1
- Input
arr1 = [2, -1, 4], arr2 = [0, 3, -2]- Output
11- Why
- For i = 1, j = 2 the value is |-1 - 4| + |3 - (-2)| + |1 - 2| = 5 + 5 + 1 = 11; the other pairs give 7 and 6.
Example 2
- Input
arr1 = [5, 5], arr2 = [1, 1]- Output
1- Why
- Both arrays are constant, so only the index distance |0 - 1| = 1 contributes.
Constraints
2 <= arr1.length == arr2.length <= 4 * 10^4-10^6 <= arr1[i], arr2[i] <= 10^6
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 10 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force (every pair) | O(n²) | O(1) |
| Optimal (expand the absolute values into 4 sign cases) | O(n) | O(1) |
1Brute force (every pair)
O(n²)There are n(n - 1) / 2 pairs, each evaluated in O(1).O(1)Only the running maximum.Evaluate the expression for every pair of indices and keep the largest value.
- For every i and every j > i, compute |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + (j - i).
- Keep the maximum.
class Solution {
public int maxAbsValExpr(int[] arr1, int[] arr2) {
int n = arr1.length, best = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int value = Math.abs(arr1[i] - arr1[j]) + Math.abs(arr2[i] - arr2[j]) + (j - i);
best = Math.max(best, value);
}
}
return best;
}
}class Solution:
def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int:
n = len(arr1)
best = 0
for i in range(n):
for j in range(i + 1, n):
value = abs(arr1[i] - arr1[j]) + abs(arr2[i] - arr2[j]) + (j - i)
best = max(best, value)
return bestclass Solution {
public:
int maxAbsValExpr(vector<int>& arr1, vector<int>& arr2) {
int n = arr1.size(), best = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int value = abs(arr1[i] - arr1[j]) + abs(arr2[i] - arr2[j]) + (j - i);
best = max(best, value);
}
}
return best;
}
};function maxAbsValExpr(arr1, arr2) {
const n = arr1.length;
let best = 0;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
const value = Math.abs(arr1[i] - arr1[j]) + Math.abs(arr2[i] - arr2[j]) + (j - i);
best = Math.max(best, value);
}
}
return best;
}2Optimal (expand the absolute values into 4 sign cases)
O(n)Four linear passes over the arrays.O(1)Only a max, a min and the answer per pass.Every |x| equals the larger of x and -x, so the whole expression equals the maximum over the sign choices p, q in {+1, -1} of f(i) - f(j) with f(k) = p·arr1[k] + q·arr2[k] + k (taking i as the larger index; the symmetric case is covered by flipping both signs). For each of the 4 sign pairs, the best f(i) - f(j) over all pairs is simply max f - min f, found in one pass.
- For each (p, q) in {(1, 1), (1, -1), (-1, 1), (-1, -1)}:
- Scan k = 0..n - 1, computing f = p·arr1[k] + q·arr2[k] + k and tracking its max and min.
- Update answer = max(answer, max - min).
- Return the answer.
class Solution {
public int maxAbsValExpr(int[] arr1, int[] arr2) {
int[][] signs = { { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
int best = 0;
for (int[] s : signs) {
int hi = Integer.MIN_VALUE, lo = Integer.MAX_VALUE;
for (int k = 0; k < arr1.length; k++) {
int f = s[0] * arr1[k] + s[1] * arr2[k] + k;
hi = Math.max(hi, f);
lo = Math.min(lo, f);
}
best = Math.max(best, hi - lo);
}
return best;
}
}class Solution:
def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int:
best = 0
for p, q in ((1, 1), (1, -1), (-1, 1), (-1, -1)):
hi = float("-inf")
lo = float("inf")
for k in range(len(arr1)):
f = p * arr1[k] + q * arr2[k] + k
hi = max(hi, f)
lo = min(lo, f)
best = max(best, hi - lo)
return bestclass Solution {
public:
int maxAbsValExpr(vector<int>& arr1, vector<int>& arr2) {
int signs[4][2] = { { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } };
int best = 0;
for (auto& s : signs) {
int hi = INT_MIN, lo = INT_MAX;
for (int k = 0; k < (int)arr1.size(); k++) {
int f = s[0] * arr1[k] + s[1] * arr2[k] + k;
hi = max(hi, f);
lo = min(lo, f);
}
best = max(best, hi - lo);
}
return best;
}
};function maxAbsValExpr(arr1, arr2) {
const signs = [[1, 1], [1, -1], [-1, 1], [-1, -1]];
let best = 0;
for (const [p, q] of signs) {
let hi = -Infinity, lo = Infinity;
for (let k = 0; k < arr1.length; k++) {
const f = p * arr1[k] + q * arr2[k] + k;
hi = Math.max(hi, f);
lo = Math.min(lo, f);
}
best = Math.max(best, hi - lo);
}
return best;
}Edge cases to test
- Two elements only: there is a single pair to check
- Constant arrays: the answer is n - 1 from the index term alone
- Negative values and values of mixed sign
- i = j gives 0, so the answer is always at least 1 when n >= 2
Hints
Hint 1
The expression is symmetric in i and j, and |x| = max(x, -x). Try writing every absolute value as a choice of sign.
Hint 2
With i > j, the index term is just i - j. The other two terms each pick a sign p, q in {+1, -1}, giving 4 cases.
Hint 3
For fixed signs the expression becomes f(i) - f(j) with f(k) = p·arr1[k] + q·arr2[k] + k. The best value for that case is max f - min f.
FAQ
What is the best time complexity for Maximum of Absolute Value Expression?
Optimal (expand the absolute values into 4 sign cases) runs in O(n) time and O(1) extra space. Four linear passes over the arrays.
Which pattern does Maximum of Absolute Value Expression use?
It is a arrays & hashing problem that uses the math / number theory pattern. Other problems with the same pattern: Bulb Switcher, Reaching Points, Water and Jug Problem.
Is there a brute force solution for Maximum of Absolute Value Expression?
Yes. Brute force (every pair) takes O(n²) time and O(1) space. Evaluate the expression for every pair of indices and keep the largest value.
Which edge cases should I test for Maximum of Absolute Value Expression?
Two elements only: there is a single pair to check; Constant arrays: the answer is n - 1 from the index term alone; Negative values and values of mixed sign; i = j gives 0, so the answer is always at least 1 when n = 2.