Simple O(N) time Java solution. No sorting needed


#1
public class Solution {
    public int solve(ArrayList<Integer> A, int B) {
        HashSet<Integer> set = new HashSet<Integer>();
        
        for(int n : A){
            if(set.contains(n)){
                return 1;
            } else {
                set.add(n+B);
                set.add(n-B);
            }
        }
        return 0;
    }
}