Question: I have a list/array containing multiple lists/arrays and I want to get a list/array containing the number of inversions in each of the individual list/arrays

I have a list/array containing multiple lists/arrays and I want to get a list/array containing the number of inversions in each of the individual list/arrays within the main list using an algorithm that is O(nlogn). How can I do this in python?

For example, I read in a list that contains 4 sub lists: [ [1,2,3] , [0,0,0] , [2,3,2,1,5] , [5,4,3,2,1] ]. The algorithm should return one list that contains all of the inversions for each sub list.

For the example shown earlier the list would be [0,0,4,10] for ASCENDING ORDER because [1,2,3] contains 0 inversions, [0,0,0] contains 0 inversions, [2,3,2,1,5] contains 4 inversions, and [5,4,3,2,1] contains 10 inversions.

For the example shown earlier the list would be [3,0,5,0] for DESCENDING ORDER because [1,2,3] contains 3 inversions, [0,0,0] contains 0 inversions, [2,3,2,1,5] contains 5 inversions, and [5,4,3,2,1] contains 0 inversions.

What is the python algorithm in O(nlogn) that can accomplish this for both ascending and descending order?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!