Talentd
FleetCode

3842. Toggle Light Bulbs

Easy71.2% acceptance1 company

You are given an array bulbs of integers between 1 and 100.

There are 100 light bulbs numbered from 1 to 100. All of them are switched off initially.

For each element bulbs[i] in the array bulbs:

  • If the bulbs[i]th light bulb is currently off, switch it on.
  • Otherwise, switch it off.

Return the list of integers denoting the light bulbs that are on in the end, sorted in ascending order. If no bulb is on, return an empty list.

Example 1:

Input: bulbs = [10,30,20,10]

Output: [20,30]

Explanation:

  • The bulbs[0] = 10th light bulb is currently off. We switch it on.
  • The bulbs[1] = 30th light bulb is currently off. We switch it on.
  • The bulbs[2] = 20th light bulb is currently off. We switch it on.
  • The bulbs[3] = 10th light bulb is currently on. We switch it off.
  • In the end, the 20th and the 30th light bulbs are on.

Example 2:

Input: bulbs = [100,100]

Output: []

Explanation:

  • The bulbs[0] = 100th light bulb is currently off. We switch it on.
  • The bulbs[1] = 100th light bulb is currently on. We switch it off.
  • In the end, no light bulb is on.

Constraints:

  • 1 <= bulbs.length <= 100
  • 1 <= bulbs[i] <= 100

Asked at 1 company

Hints open one at a time

Hint 1
Simulate as described
2:28
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def toggleLightBulbs(self, bulbs: list[int]) -> list[int]:
3
Starter codePython 3Spaces: 4
Examples
Input
[10,30,20,10]
Input
[100,100]
Your code is saved on this device.Solution