14. Count Reverse Pairs
β GFG solution to the Count Reverse Pairs problem: efficiently count pairs (i,j) where arr[i] > 2*arr[j] using modified merge sort technique. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an array arr[] of positive integers. Your task is to find the count of reverse pairs. A pair of indices (i, j) is said to be a reverse pair if both the following conditions are met:
0 β€ i < j < arr.size()arr[i] > 2 * arr[j]
The goal is to efficiently count all such pairs in the given array.
π Examples
Example 1
Input: arr[] = [3, 2, 4, 5, 1, 20]
Output: 3
Explanation: The Reverse pairs are
(0, 4), arr[0] = 3, arr[4] = 1, 3 > 2*1
(2, 4), arr[2] = 4, arr[4] = 1, 4 > 2*1
(3, 4), arr[3] = 5, arr[4] = 1, 5 > 2*1 Example 2
π Constraints
$1 \le \text{arr.size()} \le 5 \times 10^4$
$1 \le \text{arr}[i] \le 10^9$
β
My Approach
The optimal approach uses a Modified Merge Sort technique to efficiently count reverse pairs while maintaining the divide-and-conquer paradigm:
Modified Merge Sort Algorithm
Divide Phase:
Recursively divide the array into two halves until single elements remain.
Count reverse pairs in left half, right half, and cross-partition pairs.
Count Cross-Partition Pairs:
For each element in the left sorted subarray, count how many elements in the right sorted subarray satisfy the reverse pair condition.
Use two pointers technique: for each
arr[i]in left half, find allarr[j]in right half wherearr[i] > 2 * arr[j].
Merge Phase:
Merge the two sorted halves back into a single sorted array.
This ensures subsequent recursive calls work with sorted subarrays.
Optimization:
Since both halves are sorted, we can use the two-pointer technique efficiently.
Once we find the rightmost valid
jfor a giveni, all elements fromm+1tojform valid pairs witharr[i].
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n log n), where n is the size of the array. The divide phase creates O(log n) levels, and each level performs O(n) work for counting pairs and merging.
Expected Auxiliary Space Complexity: O(n), as we use temporary arrays during the merge process and the recursion stack depth is O(log n).
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated