Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: How many integers in [1, 500] are divisible by 4 or by 6 but not by 12? Run the code.

Problem: How many integers in [1, 500] are divisible by 4 or by 6 but not by 12? Run the code.
print(sum(1 for x in range(1, 501) if (x % 4 == 0 or x % 6 == 0) and x % 12 != 0))
What is printed?
  1. A. 167
  2. B. 208
  3. C. 85
  4. D. 126

Correct answer: D. 126

Multiples of 4 or 6 (lcm 12): 125 + 83 − 41 = 167; removing the 41 multiples of 12 leaves 126. Stopping at the union gives 167, adding without inclusion-exclusion gives 208, and subtracting the 12s twice gives 85.

More Coding Interview Problems questions

Exams that ask this topic