Open In App

Toggle Bulb Switcher

Last Updated : 18 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Imagine a row of N bulbs where all initially turned off. You perform a series of operations to toggle the state of these bulbs. On the first round, you turn on every bulb. On the second round, you toggle every second bulb (turning it off if it is on, and vice versa). On the third round, you toggle every third bulb. This process continues until you have made N passes, toggling every ith bulb on the ith pass. The task is to determine the number of bulbs are in on state after N rounds.

Examples:

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.

Input: N = 1
Output: 1
Explanation: At first, the bulb in in off state. After the first round, the bulb will be toggled to on state.

Illustration :


Approach:

For determining the number of bulbs that are on after N rounds, we need to consider the pattern of toggling each bulb. The idea is that a bulb will be in on state only if it has been toggled odd number of times. Now, any ith bulb will be toggled in rounds r1, r2, r3 ... where r1, r2, r3 ... are divisors of i, so we need to find only those bulbs which have odd number of divisors. So, the number of bulb which will be in on state after N rounds = count of numbers which have odd number of divisors.

To find the count of numbers which have odd number of divisors, we can say that a number has an odd number of divisors if and only if it is a perfect square. This is because divisors generally come in pairs(Eg: 1 * 12, 2 * 6, 3 * 4, 4 * 3, 6 * 2, 12 * 1 for 12). For perfect squares, one of the divisors is repeated (Eg: 1 * 9, 3 * 3, 9 * 1). Thus, the bulbs that remain on are those in positions that are perfect squares (1, 4, 9, 16, ...).The number of perfect squares less than or equal to n is the largest integer k such that k^2 <= n . This is simply the integer part of the square root of n.

Below is the implementation of the above algorithm:

C++
#include <cmath>
#include <iostream>
using namespace std;
int countBulbsOn(int n) { return (int)sqrt(n); }

int main()
{
    cout << countBulbsOn(3) << endl; // Output: 1
    cout << countBulbsOn(0) << endl; // Output: 0
    cout << countBulbsOn(1) << endl; // Output: 1
    return 0;
}
Java
public class Solution {
    public int countBulbsOn(int n)
    {
        return (int)Math.sqrt(n);
    }

    public static void main(String[] args)
    {
        Solution solution = new Solution();
        System.out.println(
            solution.countBulbsOn(3)); // Output: 1
        System.out.println(
            solution.countBulbsOn(0)); // Output: 0
        System.out.println(
            solution.countBulbsOn(1)); // Output: 1
    }
}
Python
import math


def count_bulbs_on(n):
    return int(math.sqrt(n))


# Example usage
print(count_bulbs_on(3))  # Output: 1
print(count_bulbs_on(0))  # Output: 0
print(count_bulbs_on(1))  # Output: 1
C#
using System;

public class Solution {
    public int CountBulbsOn(int n)
    {
        return (int)Math.Floor(Math.Sqrt(n));
    }

    public static void Main(string[] args)
    {
        Solution solution = new Solution();
        Console.WriteLine(
            solution.CountBulbsOn(3)); // Output: 1
        Console.WriteLine(
            solution.CountBulbsOn(0)); // Output: 0
        Console.WriteLine(
            solution.CountBulbsOn(1)); // Output: 1
    }
}
JavaScript
function countBulbsOn(n) {
    return Math.floor(Math.sqrt(n));
}

console.log(countBulbsOn(3));  // Output: 1
console.log(countBulbsOn(0));  // Output: 0
console.log(countBulbsOn(1));  // Output: 1

Output
1
0
1

Time Complexity: O(logN)
Auxiliary Space: O(1)


Next Article
Article Tags :
Practice Tags :

Similar Reads