Pair with Given Difference

Given an array of integers *nums* and an integer *k*, determine whether there are two distinct indices *i* and *j* such that *nums[i] - nums[j] = k*. Return `true` if such a pair exists, otherwise `false`.

Input Format

nums = [array of integers], k = integer

Output Format

boolean (true/false)

Constraints

  • 2 <= nums.length <= 10^5; 0 <= k <= 10^9; -10^9 <= nums[i] <= 10^9.

Examples

Example 1:

Input:

nums = [1,5,3,4,2]
k = 3

Output:

true

Explanation:

nums[4] - nums[0] = 2 - 1 = 1 (not 3), but nums[3] - nums[0] = 4 - 1 = 3, so pair exists.

Example 2:

Input:

nums = [8,12,16,4]
k = 5

Output:

false

Explanation:

No two elements differ by 5 in this array.

Loading...
Pair with Given Difference - Hashing DSA Problem