Skip to content

Lc 319 #1383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 16, 2024
Merged

Lc 319 #1383

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/Screenshot 2024-06-16 145014.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
84 changes: 84 additions & 0 deletions dsa-solutions/lc-solutions/0300-0399/0319-Bulb-Switcher
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
id: bulb-switcher
title: Bulb Switcher (LeetCode)
sidebar_label: 319-Bulb-Switcher
tags:
- Math
- Brain Teaser
description: 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.

sidebar_position: 0319
---

## Problem Description

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:** `3`
- **Output:** `1`
- **Explanation:**![Screenshot of the application](.././../../assets/BulbSwitched.png)
- 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:** `0`
- **Output:** `0`

### Example 2

- **Input:** `1`
- **Output:** `1`

### Constraints

- `0 <= n <= 10^9`

## Approach
- We can notice that a bulb's final state (on or off) depends on how many times it is toggled. Bulbs are toggled only when their positions are factors of the round number. So, the crucial insight is that a bulb ends up being on if and only if it has an odd number of factors. Mathematically, the only numbers that have an odd number of factors are perfect squares because factors usually come in pairs, and a square has a middle factor that is counted only once. For example, 9 is toggled on rounds 1, 3, and 9.

- Given this, the problem simplifies to finding out how many perfect squares are there up to n, because these will be the bulbs that remain on. The number of perfect squares up to n is simply the largest integer square root of n, because for each number x such that `x^2 <= n`, there is a corresponding perfect square.

### Solution Code

#### C++

```c++
class Solution {
public:
int bulbSwitch(int n) {
return sqrt(n);
}
};
```
#### Java
```java
class Solution {
public int bulbSwitch(int n) {
return (int) Math.sqrt(n);
}
}
```

#### Python
```python
class Solution:
def bulbSwitch(self, n: int) -> int:
return int(sqrt(n))

```

#### Conclusion
- Time Complexity
The total time complexity as O(1).

- Space Complexity
The space complexity is O(1).
Loading