{x}
blog image

Bulb Switcher

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 <= 109

Solution Explanation for Bulb Switcher

This problem can be solved efficiently using mathematical reasoning rather than simulating the toggling of bulbs.

Core Idea:

The key observation is that a bulb ends up on if and only if it's toggled an odd number of times. A bulb is toggled once for each of its divisors (factors). A number has an odd number of divisors if and only if it's a perfect square.

Why Perfect Squares?

Divisors come in pairs. For example, the number 12 has divisors (1, 12), (2, 6), (3, 4). Each divisor d is paired with n/d, where n is the number. Only when d equals n/d (meaning n is a perfect square) do we have an odd number of divisors.

Algorithm:

  1. Find Perfect Squares: Determine the number of perfect squares less than or equal to n. The largest perfect square less than or equal to n is floor(sqrt(n)).
  2. Return the Count: The number of perfect squares is the answer, as these are the bulbs that will be on after all the rounds of toggling.

Time and Space Complexity:

  • Time Complexity: O(1) - The sqrt() operation is a constant-time operation.
  • Space Complexity: O(1) - The solution uses only a constant amount of extra space.

Code Implementation (Python):

import math
 
class Solution:
    def bulbSwitch(self, n: int) -> int:
        return int(math.sqrt(n))

Code Implementation (Java):

class Solution {
    public int bulbSwitch(int n) {
        return (int) Math.sqrt(n);
    }
}

Code Implementation (C++):

#include <cmath>
 
class Solution {
public:
    int bulbSwitch(int n) {
        return (int)sqrt(n);
    }
};

Code Implementation (Go):

package main
 
import (
	"fmt"
	"math"
)
 
func bulbSwitch(n int) int {
	return int(math.Floor(math.Sqrt(float64(n))))
}
 
func main() {
	fmt.Println(bulbSwitch(3)) // Output: 1
	fmt.Println(bulbSwitch(0)) // Output: 0
	fmt.Println(bulbSwitch(1)) // Output: 1
	fmt.Println(bulbSwitch(9)) // Output: 3
 
}

Code Implementation (TypeScript):

function bulbSwitch(n: number): number {
    return Math.floor(Math.sqrt(n));
}

All the code implementations above directly calculate the floor of the square root of n to get the number of bulbs that will be on. This is the most efficient approach to this problem.