ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import bisect
a = [3, 1, 2, 5, 4]
seen = []
inv = 0
for x in a:
    inv += len(seen) - bisect.bisect_right(seen, x)
    bisect.insort(seen, x)
print(inv)
  1. A. 2
  2. B. 5
  3. C. 3
  4. D. 4

Correct answer: C. 3

For each element, count how many earlier elements are greater than it. The inversions in [3, 1, 2, 5, 4] are (3,1), (3,2) and (5,4), so the total is 3.

More Python Programming questions

Exams that ask this topic