Problem · Array
Prime Factor Visitation
Learn this problemProblem statement
Alex has a row of light bulbs that are initially either on or off. The bulb at 1-based position i has state states[i - 1], where 0 means off and 1 means on.
Process the values in numbers from left to right. For each value:
- Find its distinct prime factors.
- For every such factor
p, flip each bulb whose 1-based position is a multiple ofp.
A factor is used only once for one value, even when it appears repeatedly in that value's prime factorization. The same factor may be processed again for another value in numbers, causing its bulbs to flip again.
Return the final state of every bulb after all values have been processed.
Function
lightBulbs(states: int[], numbers: int[]) → int[]Examples
Example 1
states = [1,1,0,0,1,1,0,1,1,1]numbers = [3,4,15]return = [1,0,0,1,0,0,0,0,1,1]The distinct prime factors are {3} for 3, {2} for 4, and {3, 5} for 15.
- Factor
3flips positions3,6, and9. - Factor
2flips positions2,4,6,8, and10. - The final value
15flips multiples of3, then multiples of5.
The final states are [1,0,0,1,0,0,0,0,1,1].
Constraints
1 <= states.length <= 10^51 <= numbers.length <= 10^5- Each
states[i]is either0or1. 1 <= numbers[i] <= 10^5