> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count Number of Bad Pairs Python Solution

> Tested Python solution for LeetCode 2364 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 2364, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Math](/catalog/topics/math), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/count-number-of-bad-pairs/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2364   # by problem number
lcpy gen -s count_number_of_bad_pairs   # by problem name
```

## Problem

You are given a \<strong>0-indexed\</strong> integer array \<code>nums\</code>. A pair of indices \<code>(i, j)\</code> is a \<strong>bad pair\</strong> if \<code>i \< j\</code> and \<code>j - i != nums\[j] - nums\[i]\</code>.

Return \<em>the total number of \<strong>bad pairs\</strong> in \</em>\<code>nums\</code>.

### Examples

```
Input: nums = [4,1,3,3]
Output: 5
```

**Explanation:** The pair (0, 1) is a bad pair since 1 - 0 != 1 - 4.
The pair (0, 2) is a bad pair since 2 - 0 != 3 - 4, 2 != -1.
The pair (0, 3) is a bad pair since 3 - 0 != 3 - 4, 3 != -1.
The pair (1, 2) is a bad pair since 2 - 1 != 3 - 1, 1 != 2.
The pair (2, 3) is a bad pair since 3 - 2 != 3 - 3, 1 != 0.
There are a total of 5 bad pairs, so we return 5.

```
Input: nums = [1,2,3,4,5]
Output: 0
```

**Explanation:** There are no bad pairs.

### Constraints

* 1 \<= nums.length \<= 10^5
* 1 \<= nums\[i] \<= 10^9

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_number_of_bad_pairs/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_number_of_bad_pairs/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(n)
    def count_bad_pairs(self, nums: list[int]) -> int:
        good = 0
        seen: dict[int, int] = {}
        for i, num in enumerate(nums):
            key = num - i
            good += seen.get(key, 0)
            seen[key] = seen.get(key, 0) + 1
        n = len(nums)
        return n * (n - 1) // 2 - good
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.