C++ solution using sort


int Solution::diffPossible(const vector &A, int B) {
unordered_map<int,bool> hash;
for(int i=0;i<A.size();i++)
if(hash.find(a[i])!=hash.end()) return 1;
else hash[a[i]+B]=true;
return 0;


sorting increase the Time Complexity,
you can always check for if A[i]-B or B-A[i] is present or not.
loop for the whole array. O(N)
Yours works in O(N*logN).
Happy coding.


for checking A[i]-B or B-A[i] is present or not it takes O(n) time in vector.So yours code will run on O(n^2).


Use unordered_map but don’t sort the array.