-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreverse-pairs.cpp
57 lines (49 loc) · 1.76 KB
/
reverse-pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class Solution {
public:
int cnt = 0;
void merge(vector<int>& arr, int low, int mid, int high) {
vector<int> temp; // temporary array
int left = low; // starting index of left half of arr
int right = mid + 1; // starting index of right half of arr
// Storing elements in the temporary array in a sorted manner
while (left <= mid && right <= high) {
if (arr[left] <= arr[right]) {
temp.push_back(arr[left++]);
} else {
temp.push_back(arr[right++]);
}
}
// If elements on the left half are still left
while (left <= mid) {
temp.push_back(arr[left++]);
}
// If elements on the right half are still left
while (right <= high) {
temp.push_back(arr[right++]);
}
// Transferring all elements from temporary to arr
for (int i = low; i <= high; i++) {
arr[i] = temp[i - low];
}
}
void countPairs(vector<int>& arr, int low, int mid, int high) {
int right = mid + 1;
for (int i = low; i <= mid; i++) {
while (right <= high && arr[i] > 2LL * arr[right]) right++;
cnt += (right - (mid + 1));
}
}
void mergeSort(vector<int>& arr, int low, int high) {
if (low >= high) return;
int mid = (low + high) / 2;
mergeSort(arr, low, mid); // left half
mergeSort(arr, mid + 1, high); // right half
countPairs(arr, low, mid, high);
merge(arr, low, mid, high); // merging sorted halves
}
int reversePairs(vector<int>& nums) {
cnt = 0; // Reset the count for each new call
mergeSort(nums, 0, nums.size() - 1);
return cnt;
}
};