↓Skip to main content
  1. LeetCode/

LeetCode 3842: Toggle Light Bulbs

·1 min· ·
LeetCode Easy Hash-Table Simulation Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-02-16

  • Submissions: Wrong Answer → Accepted

Approach
#

Each appearance toggles once, so the bulbs left on are those appearing an odd number of times. Simulate with a set: remove if present, add if not (Hash Table Template 1). The answer must be ascending.

My Attempts
#

Attempt 1 returned list(light), but a set has no order. For [8, 1] the answer is [1, 8]; it returned [8, 1]. Adding sorted passed.

Solution
#

class Solution:
    def toggleLightBulbs(self, bulbs: List[int]) -> List[int]:
        on = set()
        for b in bulbs:
            on ^= {b}                      # remove if present, add if not
        return sorted(on)

Complexity
#

Time O(n + k log k) with k bulbs on at the end, space O(k).

Mistakes to Avoid
#

  • Converting a set to a list does not guarantee order; sort when the problem asks.

Interview Explanation
#

Every appearance toggles, so bulbs appearing an odd number of times stay on. I toggle with a symmetric difference on a set and return it sorted. Time O(n log n).

Related

LeetCode 3679: Minimum Discards to Balance Inventory
·3 mins
LeetCode Medium Sliding-Window Hash-Table Simulation Counting Array
LeetCode 3679 note: the rule already decides what must be discarded, so simulate it; the length-w window counts only kept arrivals, and discarded ones must not be subtracted when they leave
LeetCode 2094: Finding 3-Digit Even Numbers
·2 mins
LeetCode Easy Hash-Table Enumeration Array
LeetCode 2094 note: enumerate the even numbers 100 to 998 instead and check that each digit needed is available; Counter subtraction keeps only positives
LeetCode 3487: Maximum Unique Subarray Sum After Deletion
·2 mins
LeetCode Easy Greedy Hash-Table Array
LeetCode 3487 note: free deletion means picking distinct values; take every non-negative one, or only the maximum when all are negative
LeetCode 2099: Find Subsequence of Length K With the Largest Sum
·1 min
LeetCode Easy Array Hash-Table Sorting Heap-Priority-Queue
LeetCode 2099 note: pick the k largest values along with their indices, then sort by index to restore the original order
LeetCode 2357: Make Array Zero by Subtracting Equal Amounts
·1 min
LeetCode Easy Array Hash-Table Greedy Sorting
LeetCode 2357 note: each operation zeroes the group of the smallest positive value, so the answer is the number of distinct positive values
LeetCode 1646: Get Maximum in Generated Array
·2 mins
LeetCode Easy Array Simulation
LeetCode 1646 note: generate by the rules, nums[i // 2] at even i and nums[i // 2] + nums[i // 2 + 1] at odd i, handling n = 0 separately