Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many non-negative integer solutions (x, y, z) of x + y + z = 10 satisfy x <= 4? Run the code.

Problem: How many non-negative integer solutions (x, y, z) of x + y + z = 10 satisfy x <= 4? Run the code.
print(sum(1 for x in range(0, 11) for y in range(0, 11) for z in range(0, 11) if x + y + z == 10 and x <= 4))
What is printed?
  1. A. 66
  2. B. 55
  3. C. 35
  4. D. 45

Correct answer: D. 45

For each x from 0 to 4 there are 11 - x choices for (y, z), giving 11 + 10 + 9 + 8 + 7 = 45.

More Coding Interview Problems questions

Exams that ask this topic