ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from collections import Counter
def div_pairs(a, k):
    c = Counter(x % k for x in a)
    total = 0
    for r in c:
        s = (k - r) % k
        if r < s:
            total += c[r] * c[s]
        elif r == s:
            total += c[r] * (c[r] - 1) // 2
    return total
print(div_pairs([1, 2, 3, 4, 5, 6], 3), div_pairs([3, 3, 3, 3], 3))
  1. A. 4 6
  2. B. 5 4
  3. C. 6 6
  4. D. 5 6

Correct answer: D. 5 6

Group values by remainder mod k. Remainder 0 gives one pair from 3 and 6, and remainders 1 and 2 give 2 x 2 = 4 pairs, so 5 in total. Four values with remainder 0 give C(4,2) = 6 pairs.

More Python Programming questions

Exams that ask this topic