ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import bisect
def inv(a):
    s, c = [], 0
    for x in a:
        c += len(s) - bisect.bisect_right(s, x)
        bisect.insort(s, x)
    return c
print(inv([3, 1, 2, 3, 0]))
  1. A. 6
  2. B. 7
  3. C. 10
  4. D. 2

Correct answer: A. 6

Each value adds the count of earlier values strictly greater than it (bisect_right): pairs (3,1), (3,2), (3,0), (1,0), (2,0) and the second (3,0) make 6. bisect_left would also count the equal pair (3,3) for 7, 10 is C(5,2) and 2 counts only adjacent inversions.

More Python Programming questions

Exams that ask this topic