You are given an integer array nums and an integer k.
Return the number of index pairs (i, j) with i < j, nums[i] == nums[j], and j - i <= k.
Examples
Example 1
Input: nums = [1,2,1,2,1], k = 2
Output: 3
Explanation: The qualifying pairs are (0,2), (1,3), and (2,4). Each pair has equal values and an index gap of at most 2.
Example 2
Input: nums = [1,2,1,2,1], k = 1
Output: 0
Explanation: No two adjacent elements are equal, so no pair has an index gap of 1 or less.
Example 3
Input: nums = [5,5,5], k = 1
Output: 2
Explanation: The pairs (0,1) and (1,2) both have a gap of 1. The pair (0,2) has a gap of 2, which exceeds k, so it does not count.
Constraints
1≤nums.length≤5×104
1≤k≤nums.length
1≤nums[i]≤109
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up