Reverse Pairs
Problem Description
Given an integer array nums, return the number of important reverse pairs.
A reverse pair is a pair (i, j) where:
- 0 <= i < j < nums.length
- nums[i] > 2 * nums[j]
Example 1:
Input: nums = [1,3,2,3,1]
Output: 2
Explanation: (1,4): nums[1]=3 > 2*nums[4]=2; (3,4): nums[3]=3 > 2*1=2
Example 2:
Input: nums = [2,4,3,5,1]
Output: 3
DE Shaw Tier 1 — asked Jan 2025, Oct 2024.
Optimal: Modified merge sort O(n log n). During merge, count cross-half pairs before merging (both halves sorted, so j only moves forward — amortized O(n) per level).
Alternative: BIT over coordinate-compressed values — also O(n log n). Mention both to signal seniority.
Example Test Cases
Example 1
Input: [[1,3,2,3,1]]
Expected: 2
Example 2
Input: [[2,4,3,5,1]]
Expected: 3
Example 3
Input: [[1,1,1,1]]
Expected: 0
Run your code to see the results