Count Equal-Offset Pairs Within an Index Distance
Problem statement
Given an integer array nums and an integer k, count index pairs (i,j) satisfying 0 <= i < j < nums.length, j - i < k, and nums[i] - nums[j] = i - j.
Return the exact count as a signed 64-bit integer. When k <= 1, no pair qualifies.
Function
countEqualOffsetPairs(nums: int[], k: int) → longExamples
Example 1
nums = [3,4,5,7]k = 4return = 3The transformed values nums[i]-i are [3,3,3,4]; the first three indices form three qualifying pairs.
Example 2
nums = [1,2,3,4]k = 2return = 3Only adjacent pairs are within strict distance two, and all share the same transformed value.
Example 3
nums = [5,5,5]k = 5return = 0The transformed values are distinct, so no pair satisfies the equality.
Constraints
0 <= nums.length <= 200000.-10^9 <= nums[i] <= 10^9.0 <= k <= 10^9.