Reverse Pairs
Reverse Pairs is LeetCode 493 (Hard). Count the pairs of indices (i, j) with i < j and nums[i] > 2 * nums[j].
numsholds up to 5 × 10^4 numbers, so checking all pairs, about 1.25 billion of them, is too slow.- Values cover the whole 32-bit range, so
2 * nums[j]does not fit in a 32-bit integer.
- 1 <= nums.length <= 5 * 10^4
- -2^31 <= nums[i] <= 2^31 - 1
Intuition
Sort the array with merge sort and count during the merges. Any pair (i, j) is split by exactly one merge, the first one whose range contains both: i lands in its left half and j in its right half. Sorting inside a half never moves a number across the middle, so when that merge runs, i is still to the left of j.
With both halves sorted, one forward-only pointer counts every crossing pair. For a left value, the right values it beats form a prefix of the right half, and a bigger left value beats at least that prefix. So walk the left half in order and only ever push j forward.
"Count pairs i < j where the left value beats some function of the right value" is merge sort counting: Count Inversions, Count of Smaller Numbers After Self and Count of Range Sum all count the crossing pairs between two sorted halves.
Approach
Before reading on, take a left half [2, 4] and a right half [1, 3, 5], both sorted. Count the pairs with left > 2 × right using a pointer that never moves backwards. Then decide where that count goes inside merge sort. Aim for O(n log n).
Split and count each half
sort_count(lo, hi) returns the number of reverse pairs inside nums[lo:hi] and leaves that range sorted. It calls itself on both halves first; a range of one number has no pairs.
Sweep the pairs that cross the middle
Set j = mid. For each i in the left half, move j forward while nums[i] > 2 * nums[j], then add j - mid: that is how many right values this left value beats, all of them sitting before j.
Merge only after counting
Merge the two sorted halves into place: heapq.merge in Python, std::merge in C++, a two-pointer merge in Java. Counting has to come first, because once the halves are mixed you can no longer tell which numbers came from the left.
Add the three counts
Return the left-half count plus the right-half count plus the crossing count. reversePairs returns sort_count(0, n), the total for the whole array.
Code in Python, C++ and Java
Common pitfalls
Doubling in 32-bit arithmetic
while (j < hi && nums[i] > 2 * nums[j]) j++;
while (j < hi && nums[i] > 2LL * nums[j]) j++;
In C++ and Java, 2 * nums[j] is computed as a 32-bit int. For [2147483647, 1073741824], doubling the second value wraps to −2147483648, so the pair is counted and the answer is 1 instead of 0. Python integers never overflow.
Counting inside the merge comparison
if nums[a] > nums[b]:
count += mid - awhile j < hi and nums[i] > 2 * nums[j]:
j += 1
count += j - midThat is the Count Inversions test, left > right, not left > 2 × right. On [3, 2] it counts one pair, but 3 is not more than 4, so the answer is 0.
Halving instead of doubling
nums[i] // 2 > nums[j]
nums[i] > 2 * nums[j]
Integer division rounds down and changes the answer for odd values. On [3, 1], 3 // 2 = 1 is not more than 1, so the pair is missed, yet 3 > 2 is true and the answer is 1.
Edge cases
Doubling a negative number makes it smaller, so [-5, -5] has one pair: −5 > −10. The sweep still works, because doubling keeps the right half in sorted order.
Complexity
Reverse Pairs FAQ
How is Reverse Pairs different from Count Inversions?
Count Inversions counts pairs with nums[i] > nums[j], which is the same comparison the merge already makes, so the count can be added inside the merge loop. Reverse Pairs compares with 2 * nums[j], a different test from the merge's, so it needs its own sweep before each merge.
Can it be solved with a Fenwick tree?
Yes. Read nums left to right, and for each value count how many earlier values are greater than twice it, using a Fenwick tree over the sorted list of all values. Then add the value to the tree. It is also O(n log n), but needs a value-compression step that merge sort avoids.