-2

Let A[1...n] be an array consisting of n different numbers.

The pair (i, j) is called an inverse, If i < j and A [i] > A [j].

Example:

A := (2, 3, 8, 6, 1) => A has 5 inverses.

Task:

Write program to find the number of inverses of the array A [1..n] such that the complexity of the algorithm is O (n * logn).

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
  • Welcome to Stack Overflow! Homework questions should show effort and the current code that you have. You are unlikely to get a good response by dumping your homework question verbatim; explain what you're struggling with and provide clear debugging information. – Aurora0001 Oct 22 '16 at 15:24
  • http://stackoverflow.com/a/40001355/1040597 – Saeid Nov 03 '16 at 17:36

1 Answers1

0

This problem can be solved based on the merge sort.

strictly speaking, you should modify the procedure merge(A, B) that it returns the number of pairs (a, b) such that a in A, b in B and b > c.

As you can see the run time that is required to solve this problem is the run time of the merge sort, hence O(n * log(n))

Ayoub Falah
  • 484
  • 3
  • 19