Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Bulb Switcher problem solution

YASH PAL, 31 July 202421 January 2026

In this Leetcode Bulb Switcher problem solution, there are n bulbs that are initially off. You first turn on all the bulbs, then you turn off every second bulb. In 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.

Leetcode Bulb Switcher problem solution

Leetcode Bulb Switcher problem solution in Python.

class Solution:
    def bulbSwitch(self, n: int) -> int:
        return int(n**0.5)

Bulb Switcher problem solution in Java.

public int bulbSwitch(int n) {
        List arr = new ArrayList<>();
        for(int i = 1; i * i <= n; i++){
            
            arr.add(i*i);
        }
        
        return arr.size();
    }

Problem solution in C++.

class Solution {
public:
    int bulbSwitch(int n) {
        long long lo = 0, hi = 100000000;
        while(lo <= hi){
            long long mid = (lo + hi) / 2;
            if(mid * mid == n)return mid;
            if(mid * mid <= n){
                lo = mid + 1;
            }else{
                hi = mid - 1;
            }
        }
        return hi;
    }
};

Problem solution in C.

int bulbSwitch(int n) {
    return sqrt(n);
}

coding problems solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes