Reverse Pairs
Given an integer array nums, return the number of reverse pairs. A reverse pair is a pair (i, j) where i < j and nums[i] > 2 * nums[j].
Open official problem prompt ↗Count how many ordered pairs have a left value more than double the right value, across the whole array.
Imagine ranking cars by resale value. A reverse pair is an early car worth more than twice a later one. Sorting each half of the timeline lets you tally, for each pricey early car, how many cheap later cars it dwarfs, without checking every combination.
- Input
- nums = [1,3,2,3,1]
- Output
- 2
- Why
- The reverse pairs are (1,4): 3 > 2*1, and (3,4): 3 > 2*1; no other pair satisfies nums[i] > 2*nums[j].
1 <= nums.length <= 5 * 10^4-2^31 <= nums[i] <= 2^31 - 1