Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA

Array b = [9, 2, 7, 4, 1, 8, 3]. Count the pairs (i < j) with b[i] > 2·b[j] (a reverse pair). Run the brute-force code.

Array b = [9, 2, 7, 4, 1, 8, 3]. Count the pairs (i < j) with b[i] > 2·b[j] (a reverse pair). Run the brute-force code.
b=[9,2,7,4,1,8,3]
print(sum(1 for i in range(len(b)) for j in range(i+1,len(b)) if b[i]>2*b[j]))
What is printed?
  1. A. 13
  2. B. 9
  3. C. 8
  4. D. 4

Correct answer: C. 8

A reverse pair needs the strict inequality b[i] > 2·b[j] with i before j. Allowing equality over-counts, plain inversions count a different set, and reversing the index roles checks the wrong direction.

More Coding Interview Problems questions

Exams that ask this topic