FastPrepCount Equal-Offset Pairs Within an Index Distance

Count Equal-Offset Pairs Within an Index Distance

Google logoGoogle● MediumNEW GRADPHONE SCREEN
Learn

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) → long

Examples

Example 1

nums = [3,4,5,7]k = 4return = 3

The 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 = 3

Only adjacent pairs are within strict distance two, and all share the same transformed value.

Example 3

nums = [5,5,5]k = 5return = 0

The 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.

More Google problems

See Google hiring insights
public long countEqualOffsetPairs(int[] nums, int k) {
    // Write your code here.
}
nums[3,4,5,7]
k4
expected3
Checking account…