---
# System prepended metadata

title: 2364. Count Number of Bad Pairs
tags: [Leetcode, HashMap, Medium]

---

# 2364. Count Number of Bad Pairs
###### tags: `Leetcode` `Medium` `HashMap`
Link: https://leetcode.com/problems/count-number-of-bad-pairs/description/
## 思路
和[1814. Count Nice Pairs in an Array](https://hackmd.io/c8Ipq2IPRyiaZqYRaz69yw)很像
```(j-i) != (A[j]-A[i]) => (j-A[j]) != (i-A[i])```
## Code
```java=
class Solution {
    public long countBadPairs(int[] nums) {
        long ans = 0;
        Map<Integer, Integer> map = new HashMap<>();
        for(int i=0; i<nums.length; i++){
            ans += i-map.getOrDefault(nums[i]-i, 0);
            map.put(nums[i]-i, map.getOrDefault(nums[i]-i, 0)+1);
        }
        return ans;
    }
}
```