Skip to content
Onsite

Pair with Difference K

EasyP2Coding

nums is sorted in non-decreasing order and k is a non-negative integer. Return true if there are two different indices i and j with nums[i] - nums[j] == k, otherwise false.

Different indices may hold equal values, so k = 0 is answered true only when some value appears at least twice.

Example 1

Input
nums = [1,3,5,8], k = 5
Output
true

Example 2

Input
nums = [1,2,3], k = 10
Output
false

Example 3

Input
nums = [1,1,2], k = 0
Output
true

Constraints

  • 1 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9, sorted non-decreasing
  • 0 <= k <= 2 * 10^9

Hints

Run checks the 3 visible tests, Submit all 12.