Maximum Absolute Difference
Problem
You are given an array of N integers, A1, A2, .... AN.
Return the 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 and i != j.
Problem Constraints
1 <= N <= 100000
-109 <= A[i] <= 109
Example Input
Input 1:
A = [1, 3, -1]
Input 2:
A = [3, 2, 5, 1]
Example Output
Output 1:
5
Output 2:
4
Explanation
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.Question Link
https://www.interviewbit.com/problems/maximum-absolute-difference/
Solution
We have to find the maximum value of the function f(i,j) = |A[i]-A[j]| + |i-j|.
Solution 1: Brute Force
A simple solution is to apply two loops and calculate the maximum of the the function f.
Algorithm:
- Initialize diff=0, res=0, idxDiff=0.
- Iterate the array from left to right.
- Iterate the array from left to right.
- diff = Math.abs(A.get(i)-A.get(j));
- idxDiff = Math.abs(i-j);
- res = Math.max(res, diff+idxDiff);
- return res;
public class Solution {
public int maxArr(ArrayList<Integer> A) {
int diff = 0;
int res=0;
int idxDiff=0;
for(int i=0;i<A.size()-1;i++){
for(int j=i+1;j<A.size();j++){
diff = Math.abs(A.get(i)-A.get(j));
idxDiff = Math.abs(i-j);
res = Math.max(res, diff+idxDiff);
}
}
return res;
}
}
Complexity Analysis:
- Time Complexity: O(n^2)
- Space Complexity: O(1)
Solution 2: Optimal Solution
In brute force solution, we find each pair and then find the maximum among them. We cannot solve this question using sorting because if we sort then the index of the value also changed which we don't want.
Similarly we try to break our equation:
if A[i] > A[j] and i>j
=> A[i] - A[j] + i - j=> A[i] + i - (A[j]+j)
if A[i] > A[j] and j>i
=> A[i] - A[j] + j - i
=> A[i] - i - (A[j] - j)
if A[j] > A[i] and i > j
=> A[j] - A[i] + i - j
=> A[j] - j - (A[i] - i)
if A[j] > A[i] and j > i
=> A[j] - A[i] + j - i
=> A[j] + j -(A[i] + i)
From the above four equation which we got after removing mod from the equation. we observe a pattern that the equation(1) and equation(4) are same i.e Element of array + index. equation(1) is the reciprocal of the equation(4). the equation(2) and equation(3) are same i.e element of array - index. equation(2) is the reciprocal of the equation(3).
So, we conclude that we have two equations:
- A[i] + i - (A[j] + j)
- A[i] - i - (A[j] - j)
Maximum of above two equation will give us the answer.
To find the maximum, we also need to maximize the both equation.
To maximize the equation:
- A[i] + i - (A[j] + j)
we need to maximize the 1st part i.e (A[i] + i) and minimize the 2nd part i.e (A[j] + j).
we need to find the maximum and minimum value of the (element of array + index) .
- A[i] - i - (A[j] - j)
we need to maximize the 1st part i.e (A[i] - i) and minimize the 2nd part i.e (A[j] - j).
we need to find the maximum and minimum value of the (element of array - index).
As we can see, we have two equations A[i] + i and A[i] - i and find the maximum and minimum of these two.
Algorithm:
- Initialize the max1 = Integer.MIN_VALUE, min1 = Integer.MAX_VALUE,
- Iterate from left to right.
- val1 = A.get(i) + i.
- max1 = Math.max(max1,val1);
- min1 = Math.min(min1, val1);
- val2 = A.get(i) - i.
- max2 = Math.max(max2, val2);
- min2 = Math.min(min2, val2);
- ans1 = max1 - min1.
- ans2 = max2 - min2.
- return Math.max(ans1, ans2).
Here is the solution:
public class Solution {
public int maxArr(ArrayList<Integer> A) {
int max1 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE;
int max2 = Integer.MIN_VALUE;
int min2 = Integer.MAX_VALUE;
int val1=0,val2=0;
for(int i=0;i<A.size();i++){
val1 = A.get(i)+i;
max1 = Math.max(max1,val1);
min1 = Math.min(min1,val1);
val2 = A.get(i)-i;
max2 = Math.max(max2,val2);
min2 = Math.min(min2,val2);
}
int ans1 = max1-min1;
int ans2 = max2-min2;
return Math.max(ans1,ans2);
}
}
Complexity Analysis:
- Time Complexity: O(n)
- Space Complexity: O(1)
Comments
Post a Comment