Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 B

There are 100 bulbs, all off. For each i from 1 to 100, pass i toggles every bulb whose number is a multiple of i. How many bulbs are on at the end?

  1. A. 10
  2. B. 9
  3. C. 50
  4. D. 25

Correct answer: A. 10

A bulb is toggled once per divisor and stays on only with an odd number of divisors, which happens exactly for perfect squares: 1, 4, ..., 100 = 10. Forgetting 100 gives 9, 50 assumes alternate bulbs, and 25 counts primes, which are toggled twice and end off.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic