Description

Given an integer array nums and an integer k, return true if there are two distinct indices i and j such that nums[i] == nums[j] and abs(i - j) <= k.

Examples

Input:nums = [1,2,3,1], k = 3
Output:true
Explanation:

1s at indices 0 and 3, distance <= 3.

Input:nums = [1,0,1,1], k = 1
Output:true
Explanation:

nums[2] and nums[3] are both 1, and |2-3| = 1 <= k.

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

Duplicate values exist (1,2,3 each appear twice), but the closest duplicate indices are 3 apart, which exceeds k=2.

Constraints

  • 1 ≤ nums.length ≤ 10⁵

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!