LeetCode - Bulb Switcher Solution - The Coding Shala

Home >> LeetCode >> Bulb Switcher

LeetCode - Bulb Switcher Solution

In this post, we will discuss LeetCode's Bulb Switcher Problem and its solution in Java.

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 i-th round, you toggle every i bulb. For the n-th round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Example:
Input: 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.
Practice this problem on LeetCode(Click Here).

Java Solution of Bulb Switcher Problem

Let's understand the problem first. All the blubs are off at starting. After the first round, all the blub are in on state(1,2,3,4,5,... on blubs).
After the second round, every second blub will change their state which means blub at position 2,4,6,8,.. change their state. after the third round, every third bulb 3,6,9,12,...(multiple of 3) change their state.
Now we can see if we perform odd numbers of operation of a blub then it will be in On state because starting state is off. Blub one(1) is always on because only one operation can be performed on this.
Now here we have to find the number of operations on each blub if it's odd then it will be on. We can count the number of operations by using the factors of a number. If the count of a number's factor is odd then blub will be in On state.
Bulb Switcher LeetCode
let's take an example:
n=9 let's count the factors of the number-
1 - 1
2 - 1,2
3 - 1,3
4 - 1,2,4
5 - 1,5
6 - 1,2,3,6
7 - 1,7
8 - 1,2,4,8
9 - 1,3,9

We can see here only number 1,4,9 has the odd number of factors so these blub will be in on state. We know prime number has only two factors(1 and number) so they always in off state and factors always comes in pair for example 8(1 and 8, 2 and 4) except number that has perfect square root for example 9(3*3, 1 and 9) so in this case, 3 comes only once.
So basically we need to calculate how many square numbers are there within a given number. These blub are always on- 1,4,9,16,25,...and so on.

Java Program: 

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


Other Posts You May Like
Please leave a comment below if you like this post or found some error, it will help me to improve my content.

Comments

Popular Posts from this Blog

Shell Script to find sum, product and average of given numbers - The Coding Shala

Add two numbers in Scala - The Coding Shala

Shell Script to Create a Simple Calculator - The Coding Shala

Goal Parser Interpretation LeetCode Solution - The Coding Shala

New Year Chaos Solution - The Coding Shala