Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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^6Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3,4] [-1,4,5,6]
[1,-2,-5,0,10] [0,-2,-1,-7,-4]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1131: Maximum of Absolute Value Expression
class Solution {
public int maxAbsValExpr(int[] arr1, int[] arr2) {
int[] dirs = {1, -1, -1, 1, 1};
final int inf = 1 << 30;
int ans = -inf;
int n = arr1.length;
for (int k = 0; k < 4; ++k) {
int a = dirs[k], b = dirs[k + 1];
int mx = -inf, mi = inf;
for (int i = 0; i < n; ++i) {
mx = Math.max(mx, a * arr1[i] + b * arr2[i] + i);
mi = Math.min(mi, a * arr1[i] + b * arr2[i] + i);
ans = Math.max(ans, mx - mi);
}
}
return ans;
}
}
// Accepted solution for LeetCode #1131: Maximum of Absolute Value Expression
func maxAbsValExpr(arr1 []int, arr2 []int) int {
dirs := [5]int{1, -1, -1, 1, 1}
const inf = 1 << 30
ans := -inf
for k := 0; k < 4; k++ {
a, b := dirs[k], dirs[k+1]
mx, mi := -inf, inf
for i, x := range arr1 {
y := arr2[i]
mx = max(mx, a*x+b*y+i)
mi = min(mi, a*x+b*y+i)
ans = max(ans, mx-mi)
}
}
return ans
}
# Accepted solution for LeetCode #1131: Maximum of Absolute Value Expression
class Solution:
def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int:
dirs = (1, -1, -1, 1, 1)
ans = -inf
for a, b in pairwise(dirs):
mx, mi = -inf, inf
for i, (x, y) in enumerate(zip(arr1, arr2)):
mx = max(mx, a * x + b * y + i)
mi = min(mi, a * x + b * y + i)
ans = max(ans, mx - mi)
return ans
// Accepted solution for LeetCode #1131: Maximum of Absolute Value Expression
struct Solution;
impl Solution {
fn max_abs_val_expr(arr1: Vec<i32>, arr2: Vec<i32>) -> i32 {
let n = arr1.len();
let max1 = (0..n).map(|i| arr1[i] + arr2[i] + i as i32).max().unwrap();
let min1 = (0..n).map(|i| arr1[i] + arr2[i] + i as i32).min().unwrap();
let max2 = (0..n).map(|i| arr1[i] + arr2[i] - i as i32).max().unwrap();
let min2 = (0..n).map(|i| arr1[i] + arr2[i] - i as i32).min().unwrap();
let max3 = (0..n).map(|i| arr1[i] - arr2[i] + i as i32).max().unwrap();
let min3 = (0..n).map(|i| arr1[i] - arr2[i] + i as i32).min().unwrap();
let max4 = (0..n).map(|i| arr1[i] - arr2[i] - i as i32).max().unwrap();
let min4 = (0..n).map(|i| arr1[i] - arr2[i] - i as i32).min().unwrap();
vec![max1 - min1, max2 - min2, max3 - min3, max4 - min4]
.into_iter()
.max()
.unwrap()
}
}
#[test]
fn test() {
let arr1 = vec![1, 2, 3, 4];
let arr2 = vec![-1, 4, 5, 6];
let res = 13;
assert_eq!(Solution::max_abs_val_expr(arr1, arr2), res);
let arr1 = vec![1, -2, -5, 0, 10];
let arr2 = vec![0, -2, -1, -7, -4];
let res = 20;
assert_eq!(Solution::max_abs_val_expr(arr1, arr2), res);
}
// Accepted solution for LeetCode #1131: Maximum of Absolute Value Expression
function maxAbsValExpr(arr1: number[], arr2: number[]): number {
const dirs = [1, -1, -1, 1, 1];
const inf = 1 << 30;
let ans = -inf;
for (let k = 0; k < 4; ++k) {
const [a, b] = [dirs[k], dirs[k + 1]];
let mx = -inf;
let mi = inf;
for (let i = 0; i < arr1.length; ++i) {
const [x, y] = [arr1[i], arr2[i]];
mx = Math.max(mx, a * x + b * y + i);
mi = Math.min(mi, a * x + b * y + i);
ans = Math.max(ans, mx - mi);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.