LeetCode in Kotlin

3842. Toggle Light Bulbs

Easy

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:

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:

Example 2:

Input: bulbs = [100,100]

Output: []

Explanation:

Constraints:

Solution

class Solution {
    fun toggleLightBulbs(bulbs: MutableList<Int>): MutableList<Int> {
        val on = BooleanArray(101)
        for (b in bulbs) {
            on[b] = !on[b]
        }
        val ans: MutableList<Int> = ArrayList<Int>()
        for (i in 1..100) {
            if (on[i]) {
                ans.add(i)
            }
        }
        return ans
    }
}