Problem 100
Pair With Given Difference
Easy · 10 pts · Array, Hash Table, Two Pointers
Problem
You are given an array of N integers and a non-negative integer D. Decide whether there are two elements at different positions whose difference is exactly D, that is, A[i] - A[j] = D for some i != j. Put the values in a hash set (or sort the array and use two pointers). For each value x, check whether x + D is present. When D = 0, you need a value that appears at least twice, because the two positions must be different.
Input
First line: integers N and D. Second line: N space-separated integers.
Output
Print YES or NO.
Constraints
1 <= N <= 100000; 0 <= D <= 1000000000; -1000000000 <= A[i] <= 1000000000
Example input
6 78 5 20 3 2 50 80
Example output
YES
Explanation
In 5 20 3 2 50 80 with D = 78, the elements 80 and 2 satisfy 80 - 2 = 78, so the answer is YES.