Search
Maximum Absolute Difference - Interviewbit Solution
Updated: Sep 8, 2020
Problem: Maximum Absolute Difference
Problem Description:
You are given an array of N integers, A1, A2 ,…, AN. Return maximum value of f(i, j) for all 1 ≤ i, j ≤ N.
f(i, j) is defined as |A[i] - A[j]| + |i - j|, where |x| denotes absolute value of x.For example,
A=[1, 3, -1]
f(1, 1) = f(2, 2) = f(3, 3) = 0
f(1, 2) = f(2, 1) = |1 - 3| + |1 - 2| = 3
f(1, 3) = f(3, 1) = |1 - (-1)| + |1 - 3| = 4
f(2, 3) = f(3, 2) = |3 - (-1)| + |2 - 3| = 5
So, we return 5.Solution:



https://casino-online-ohne-limit.de caught my eye recently and it got me thinking about how the four-case transformation in this problem (A[i]+i, A[i]-i, and their negatives) is really the whole trick—once you see that max(|A[i]-A[j]| + |i-j|) splits cleanly into those expressions, the O(N) solution feels almost obvious. I spent way too long trying to reason about it geometrically before realizing the algebra does all the work. Curious whether the InterviewBit expected solution also walks through the sign combinations or just jumps straight to the final formula.