Maximum Absolute Difference
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,
Explaination:
f(i, j) = |A[i] - A[j]| + |i - j|
can be written in 4 ways (Since we are looking at max value, we don’t even care if the value becomes negative as long as we are also covering the max value in some way).
Note that case 1 and 4 are equivalent and so are case 2 and 3.
We can construct two arrays with values: A[i] + i
and A[i] - i
. Then, for above 2 cases, we find the maximum value possible. For that, we just have to store minimum and maximum values of expressions A[i] + i
and A[i] - i
for all i
.
Last updated