Count Number of Bad Pairs

Medium Topics Company Tags

For indices i < j in nums, call the pair bad when the index difference j - i differs from the value difference nums[j] - nums[i]. Return the number of bad pairs. The answer can exceed the range of a signed 32-bit integer.

Example 1:

Input: nums = [1]

Output: 0

Explanation: One entry cannot form a pair of indices, so there are no bad pairs.

Example 2:

Input: nums = [1,1]

Output: 1

Explanation: For the only pair (0, 1), the index difference is 1 but the value difference is 0. The pair is bad.

Example 3:

Input: nums = [1,2]

Output: 0

Explanation: For the only pair (0, 1), both the index difference and the value difference are 1. The pair is not bad.

Constraints:

  • 1 <= nums.length <= 100000
  • 1 <= nums[i] <= 1000000000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.