We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
// Merge and count inversions
long long mergeAndCount(int arr[], int temp[], int left, int mid, int right) {
long long inv_count = 0;
int i = left;
int j = mid;
int k = left;
while (i <= mid - 1 && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
inv_count += (mid - i);
}
}
while (i <= mid - 1)
temp[k++] = arr[i++];
while (j <= right)
temp[k++] = arr[j++];
for (i = left; i <= right; i++)
arr[i] = temp[i];
return inv_count;
}
// Merge sort with inversion count
long long mergeSortAndCount(int arr[], int temp[], int left, int right) {
long long inv_count = 0;
if (left < right) {
int mid = (left + right) / 2;
Merge Sort: Counting Inversions
You are viewing a single comment's thread. Return to all comments →
include
include
// Merge and count inversions long long mergeAndCount(int arr[], int temp[], int left, int mid, int right) { long long inv_count = 0; int i = left; int j = mid; int k = left;
}
// Merge sort with inversion count long long mergeSortAndCount(int arr[], int temp[], int left, int right) { long long inv_count = 0; if (left < right) { int mid = (left + right) / 2;
}
int main() { int t; scanf("%d", &t);
}