ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
from collections import Counter
a = [1, 1, 2, 3, 3]
c = Counter(a)
print(sum(c[x] * c[4 - x] for x in c))
  1. A. 9
  2. B. 5
  3. C. 8
  4. D. 4

Correct answer: A. 9

The sum counts ordered pairs (i, j) whose values add to 4, including pairs of equal values. Value 1 pairs with 3 in 2 x 2 = 4 ways, value 2 pairs with itself in 1 way, and value 3 pairs with 1 in 4 more ways.

More Python Programming questions

Exams that ask this topic