Efficient naive solution


#1

//In this method worst case scenario is still O(n^2) but average time decreases drastically

int Solution::diffPossible(vector &A, int B) {
int n=A.size();
if(n==1) return 0;
for(int i=n-1;i>=1;i–){
if(A[i]<B) break; //very important as loop will uselessly go on if not terminated at this redundant point

    int j=0;
    
    while(j<i){
        if((A[i]-A[j])==B) return 1;
    
        j++;
    }
}

return 0;

}