O(n) time & O(1) space... JAVA Solution

facebook
Tags: #<Tag:0x00007f24275bef08>

#1
public int diffPossible(ArrayList<Integer> A, int B) {
    
    int i=0, j=1;
    while(j < A.size()){
        
        if(i!=j && A.get(j)-A.get(i) == B)
            return 1;
        else if(A.get(j)-A.get(i) > B)
            i++;
        else
            j++;
        
    }
    
    return 0;
}