Algorithms and Data Structures · Senior · Question 69 of 75

Which approach counts inversions (pairs i < j with a[i] > a[j]) in O(n log n)?

Explanation

When an element from the right half is merged before the remaining left elements, it forms an inversion with each of them. A Fenwick tree over values also works in O(n log n).

Further reading

Problem with a question

Algorithms and Data Structures · Senior · question 69

Test yourself

Take the Algorithms and Data Structures · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz