There are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb.
On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb.
Return the number of bulbs that are on after n rounds.
Example 1:
Input: n = 3 Output: 1 Explanation: At first, the three bulbs are [off, off, off]. After the first round, the three bulbs are [on, on, on]. After the second round, the three bulbs are [on, off, on]. After the third round, the three bulbs are [on, off, off]. So you should return 1 because there is only one bulb is on.
Example 2:
Input: n = 0 Output: 0
Example 3:
Input: n = 1 Output: 1
Constraints:
0 <= n <= 109Problem Overview: You have n bulbs initially turned off. On round 1 every bulb is toggled, on round 2 every second bulb is toggled, on round 3 every third bulb, and so on until round n. The task is to determine how many bulbs remain on after all rounds finish.
Approach 1: Counting Perfect Squares by Iteration (O(√n) time, O(1) space)
The key observation is that a bulb toggles once for every divisor of its index. For example, bulb 12 toggles on rounds 1, 2, 3, 4, 6, and 12. Most numbers have divisors in pairs such as (2,6) or (3,4), which results in an even number of toggles and the bulb ends up off. Only perfect squares have an odd number of divisors because one divisor pair collapses (e.g., 3 × 3). That means bulbs at positions 1, 4, 9, 16... stay on. Iterate i while i * i ≤ n and count how many perfect squares exist.
This approach directly models the mathematical insight without relying on built-in square root functions. It works well when you want explicit control over iteration or when implementing the logic in constrained environments. The algorithm performs about √n iterations and uses constant memory.
Approach 2: Perfect Square Insight (O(1) time, O(1) space)
The observation above leads to a simpler solution: the number of bulbs that remain on equals the number of perfect squares less than or equal to n. That value is simply ⌊√n⌋. Instead of simulating toggles, compute the integer square root of n and return it.
This converts a simulation problem into a pure math calculation. The toggle pattern becomes irrelevant once you recognize the divisor parity property of numbers. Because the algorithm performs only a single square root operation, the runtime is effectively constant with O(1) space. This is the intended optimal solution and a classic brainteaser style problem frequently seen in interviews that test pattern recognition rather than brute-force simulation.
Recommended for interviews: Interviewers expect the perfect square observation. Mentioning that bulbs toggle once per divisor demonstrates understanding of the mechanics, while deriving the ⌊√n⌋ formula shows strong problem-solving ability. Starting with the divisor reasoning and then simplifying to the math solution communicates both intuition and optimization.
In this approach, we observe that bulbs that remain on are those that are toggled an odd number of times. Typically, a bulb indexed at i is toggled once for every divisor it has. Numbers which are perfect squares have an odd number of divisors (since one of its divisors is repeated, for example, the square root). Hence, the number of bulbs that will remain on is determined by the number of perfect squares up to n. Therefore, the number of bulbs left on is the integer part of the square root of n.
The solution uses the math library to calculate the square root of n and casts it to an integer, thereby getting the count of perfect squares less than or equal to n.
Time Complexity: O(1) as computing the square root and type casting are constant time operations.
Space Complexity: O(1) since no additional space is used.
This approach involves explicitly counting all perfect squares up to n by probing through potential perfect squares. Starting from 1, we continue as long as i*i is less than or equal to n, incrementing the counter each time. This direct enumeration is straightforward and still only traverses a limited range due to the root domain.
Iterate with a simple for-loop, counting perfect squares by incrementing the square root, i, until i*i surpasses n.
Time Complexity: O(√n), iterates through the square roots from 1 to √n.
Space Complexity: O(1) uses fixed amount of additional space.
We can number the n bulbs as 1, 2, 3, cdots, n. For the i-th bulb, it will be operated in the d-th round if and only if d is a factor of i.
For a number i, the number of its factors is finite. If the number of factors is odd, the final state is on; otherwise, it is off.
Therefore, we only need to find the number of numbers from 1 to n with an odd number of factors.
For a number i, if it has a factor d, then it must have a factor i/d. Therefore, numbers with an odd number of factors must be perfect squares.
For example, the factors of the number 12 are 1, 2, 3, 4, 6, 12, and the number of factors is 6, which is even. For the perfect square number 16, the factors are 1, 2, 4, 8, 16, and the number of factors is 5, which is odd.
Therefore, we only need to find how many perfect squares there are from 1 to n, which is \lfloor \sqrt{n} \rfloor.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Perfect Square Insight | Time Complexity: O(1) as computing the square root and type casting are constant time operations. |
| Counting Perfect Squares by Iteration | Time Complexity: O(√n), iterates through the square roots from 1 to √n. |
| Mathematics | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Counting Perfect Squares by Iteration | O(√n) | O(1) | When you want to explicitly count perfect squares without using built-in sqrt functions |
| Perfect Square Insight (Math) | O(1) | O(1) | Optimal interview solution using the observation that only perfect square indices remain on |
Bulb Switcher (Leetcode 319) - Medium (Hindi) • The Hustling Engineer • 10,229 views views
Watch 9 more video solutions →Practice Bulb Switcher with our built-in code editor and test cases.
Practice on FleetCode