Data Structures & AlgorithmsHardProduct Company SDE · ICPC regional

Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations.

Count the 4-element subsets of {1, 2, ..., 12} whose sum is divisible by 5. Count by brute force over combinations.
from itertools import combinations
print(sum(1 for t in combinations(range(1,13),4) if sum(t)%5==0))
What is printed?
  1. A. 99
  2. B. 126
  3. C. 2376
  4. D. 44

Correct answer: A. 99

Subsets are unordered, so combinations are the right enumeration and the divisibility test is on the sum by 5. Testing divisibility by 4 is a different condition, counting ordered arrangements multiplies the count by 24, and using 3-element subsets is a different size.

More Coding Interview Problems questions

Exams that ask this topic