Pair with the given difference
Given an array and a number N, find if there exists a pair with the given difference.
Example: arr = [5, 20, 3, 2, 50, 80], N = 78 → Output: true (80-2=78)
Sample Input
—
Sample Output
—
Constraints
- 1 <= N <= 10^5
- 1 <= arr[i] <= 10^5
Topics
Hash Set
function findPair(arr, n) {
const set = new Set(arr);
for (const x of arr) if (set.has(x + n) || set.has(x - n)) return true;
return false;
}
Time: O(n) | Space: O(n)
Saved in this browser only. Private to you.