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 Sort: Counting Inversions
- Discussions
Merge Sort: Counting Inversions
Merge Sort: Counting Inversions
Sort by
recency
|
509 Discussions
|
Please Login in order to post a comment
include
include
using namespace std;
long long mergeAndCount(vector& arr, vector& temp, int left, int mid, int right) {
}
long long mergeSortAndCount(vector& arr, vector& temp, int left, int right) {
}
long long countInversions(vector& arr) {
}
int main() {
}
Java Solution
O(n log(n))
C++ solution. Solving this by merge sort is probably the quickest way, as it is done in O(nlogn) time, as opposed to other methods most of which run in O(n^2).
An addition to the merge sort algorithm made specifically for this question is that we need to keep track of the number of swaps made in the algorithm.