Tap here to switch tabs
Problem
Ask
Submissions

Problem: Bulb Switcher

med
30 min
Explore the Bulb Switcher problem which involves toggling bulbs through multiple rounds. Learn to analyze and implement an efficient solution using mathematical insight to determine how many bulbs stay on at the end. This lesson enhances your ability to solve pattern-based problems in code.

Statement

You are given n bulbs, all initially turned off. You perform n rounds of toggling operations:

  • In round 11, you turn on every bulb.

  • In round 22, you toggle every 2nd2^{nd} bulb.

  • In round 33, you toggle every 3rd3^{rd} bulb.

  • In general, during round i, you toggle every i-th bulb (turning it on if it is off, or turning it off if it is on).

  • In round n, you toggle only the last bulb.

Return the number of bulbs that remain on after all n rounds have been completed.

Constraints:

  • 00 \leq n 109\leq 10^9

Tap here to switch tabs
Problem
Ask
Submissions

Problem: Bulb Switcher

med
30 min
Explore the Bulb Switcher problem which involves toggling bulbs through multiple rounds. Learn to analyze and implement an efficient solution using mathematical insight to determine how many bulbs stay on at the end. This lesson enhances your ability to solve pattern-based problems in code.

Statement

You are given n bulbs, all initially turned off. You perform n rounds of toggling operations:

  • In round 11, you turn on every bulb.

  • In round 22, you toggle every 2nd2^{nd} bulb.

  • In round 33, you toggle every 3rd3^{rd} bulb.

  • In general, during round i, you toggle every i-th bulb (turning it on if it is off, or turning it off if it is on).

  • In round n, you toggle only the last bulb.

Return the number of bulbs that remain on after all n rounds have been completed.

Constraints:

  • 00 \leq n 109\leq 10^9