Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many ways are there to choose 4 distinct numbers from 1..12 with sum 26? Run the code.

Problem: How many ways are there to choose 4 distinct numbers from 1..12 with sum 26? Run the code.
from itertools import combinations
print(sum(1 for c in combinations(range(1, 13), 4) if sum(c) == 26))
What is printed?
  1. A. 33
  2. B. 69
  3. C. 23
  4. D. 41

Correct answer: A. 33

Enumerating the 495 four-subsets of 1..12 finds 33 with sum 26. Allowing repeated numbers gives 69, using 1..11 gives 23, and using 1..13 gives 41.

More Coding Interview Problems questions

Exams that ask this topic