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

# Reverse Pairs Python Solution with Tests

> Tested Python solution for LeetCode 493 with 22 pytest cases. Generate a practice environment with lcpy.

LeetCode 493, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Binary Search](/catalog/topics/binary-search), [Divide and Conquer](/catalog/topics/divide-and-conquer), [Binary Indexed Tree](/catalog/topics/binary-indexed-tree), [Segment Tree](/catalog/topics/segment-tree), Merge Sort, [Ordered Set](/catalog/topics/ordered-set), Treap. [View on LeetCode](https://leetcode.com/problems/reverse-pairs/description/).

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

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

## Problem

Given an integer array `nums`, return the number of **reverse pairs** in the array.

A reverse pair is a pair `(i, j)` where:

* `0 <= i < j < nums.length` and
* `nums[i] > 2 * nums[j]`.

### Examples

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

**Explanation:** The reverse pairs are:
(1, 4) --> nums\[1] = 3, nums\[4] = 1, 3 > 2 \* 1
(3, 4) --> nums\[3] = 3, nums\[4] = 1, 3 > 2 \* 1

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

**Explanation:** The reverse pairs are:
(1, 4) --> nums\[1] = 4, nums\[4] = 1, 4 > 2 \* 1
(2, 4) --> nums\[2] = 3, nums\[4] = 1, 3 > 2 \* 1
(3, 4) --> nums\[3] = 5, nums\[4] = 1, 5 > 2 \* 1

### Constraints

* 1 \<= nums.length \<= 5 \* 10^4
* -2^31 \<= nums\[i] \<= 2^31 - 1

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n log n)
    # Space: O(n)
    def reverse_pairs(self, nums: list[int]) -> int:
        def merge_sort(values: list[int]) -> tuple[list[int], int]:
            if len(values) <= 1:
                return values, 0
            mid = len(values) // 2
            left, left_pairs = merge_sort(values[:mid])
            right, right_pairs = merge_sort(values[mid:])
            pairs = left_pairs + right_pairs
            j = 0
            for x in left:
                while j < len(right) and x > 2 * right[j]:
                    j += 1
                pairs += j
            merged: list[int] = []
            i = 0
            j = 0
            while i < len(left) and j < len(right):
                if left[i] <= right[j]:
                    merged.append(left[i])
                    i += 1
                else:
                    merged.append(right[j])
                    j += 1
            merged.extend(left[i:])
            merged.extend(right[j:])
            return merged, pairs

        _, total = merge_sort(nums)
        return total
```

## Complexity

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

## Tags


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