C++ solution using sort


#1

int Solution::diffPossible(const vector &A, int B) {
unordered_map<int,bool> hash;
vectora=A;
sort(a.begin(),a.end());
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;
}


#2

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.


#3

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).


#4

Use unordered_map but don’t sort the array.