Simple C++ O(n) Solution With 2 pointers


#1

int Solution::solve(vector &A, int B) {

sort(A.begin() ,A.end());
int i=0,j=0;
while(i< A.size() && j<A.size())
{
    if(A[j]-A[i] == B && i!=j )
    {
        //cout<<A[i] << " "<< A[j] <<" " << B <<endl;
        return 1;
    }
    else
    if(A[j]-A[i] < B)
      j++;
    else
        i++;
    
}
return 0;

}


#2

You have sorted tha array. It is O(nlogn)