Data Structures & AlgorithmsHardCodeforces Div2 B · GATE CS

A row of 100 lights starts off. For each i from 1 to 100, the lights at positions that are multiples of i are toggled. How many lights are ON at the end?

  1. A. 11
  2. B. 50
  3. C. 10
  4. D. 20

Correct answer: C. 10

A light ends on exactly when it has been toggled an odd number of times, i.e. its position has an odd number of divisors, which happens only for perfect squares. Counting multiples of 2 or using n/2 misses this divisor-parity fact.

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

Exams that ask this topic