Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code.

Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code.
a = [1, 3, 2, 5, 4, 6]
print(sum(1 for i in range(len(a)) for j in range(i + 1, len(a)) if a[i] > a[j]))
What is printed?
  1. A. 2
  2. B. 3
  3. C. 1
  4. D. 4

Correct answer: A. 2

Only the adjacent swapped pairs (3,2) and (5,4) form inversions.

More Coding Interview Problems questions

Exams that ask this topic