Data Structures & AlgorithmsModerateCAT · TCS NQT

Problem: How many integers from 1 to 100 are divisible by exactly one of 2 and 3?

  1. A. 67
  2. B. 83
  3. C. 51
  4. D. 16

Correct answer: C. 51

Multiples of 2: 50, of 3: 33, of both (6): 16, so exactly one is 50 + 33 − 2·16 = 51. The union is 67, ignoring the overlap gives 83, and 16 counts the numbers divisible by both.

More Coding Interview Problems questions

Exams that ask this topic