[InterviewBit] Maximum Absolute Difference

Maximum Absolute Difference

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
int Solution::maxArr(vector<int> &A) {
int mi = -A[0] - 1, ma = A[0] - 1;
int res = INT_MIN;
for(int i = 1; i < A.size(); i++) {
res = max({res, A[i] + i + 1 + mi, -A[i] + i + 1 + ma});
mi = max(mi, -A[i] - i - 1);
ma = max(ma, A[i] - i - 1);
}
return res;
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/10/14/PS/interviewbit/maximum-absolute-difference/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.