algorithms – Find all inversions in a permutation

1a. Find all inversions in the permutation (5, 3, 6, 2, 7, 8, 4, 1). What are the consecutive inversions?

1b. Now, write a Python function find_inv (perm) that returns a list (in
order) of all non-consecutive inversions of the given permutation. For example,
find_inv ([2, 4, 1, 3, 5]) should come back [(2, 1), (4, 3)]. You can assume perm
is indeed a permutation of 1, 2 ,. . . , not; GIGO otherwise. Current time should be
Sure
2
). Include comments explaining why it is impossible to do better than O (n
2
).