[InterviewBit] Diffk

Diffk

  • Time :
  • Space :
1
2
3
4
5
6
7
8
9
10
11
12
13
int Solution::diffPossible(vector<int> &A, int B) {
int l = 0, r = 1, n = A.size();
B = abs(B);
while(r < n) {
while(r < n and A[l] + B > A[r]) r++;
if(l == r) r++;
if(r >= n) return false;
if(A[l] + B == A[r] and l != r) return true;
while(l < r and A[l] + B < A[r]) l++;
}
return false;
}

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