Prime Factor Visitation
Learn this problemProblem statement
A row contains n bulbs. 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 x:
- Find the distinct prime factors of
x. - For every such factor
p, flip every bulb whose 1-based position is a multiple ofp:p,2p,3p, and so on while the position is at mostn.
Each occurrence of a prime factor is processed independently. If the same factor appears for multiple values in numbers, its affected bulbs are flipped again each time.
Return the final state of every bulb after all values have been processed.
Function
lightsBulbs(states: int[], numbers: int[]) → int[]Examples
Example 1
states = [0, 1, 1, 0, 1, 1, 0, 1, 1, 1]numbers = [3, 8, 6]return = [0, 1, 1, 0, 1, 1, 0, 1, 1, 1]The distinct prime-factor sets are {3}, {2}, and {2, 3}. Across the full list, the multiples of 2 are flipped twice and the multiples of 3 are flipped twice. Every affected bulb therefore returns to its initial state.
Example 2
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 resulting states are [1, 0, 0, 1, 0, 0, 0, 0, 1, 1].
Constraints
1 ≤ states.length ≤ 10^51 ≤ numbers.length ≤ 10^5states[i]is either0or1.1 ≤ numbers[i] ≤ 10^9
More Citadel problems
- Limit Order Book Matching EnginePHONE SCREEN · Seen Jul 2026
- Minimum Changes for a Periodic PalindromeOA · Seen Jul 2026
- Minimum Image Processing CostOA · Seen Jul 2026
- Minimum Path Sum to Target in Binary TreePHONE SCREEN · Seen Apr 2026
- Minimum Time to Process RequestsOA · Seen Mar 2026
- Process SchedulingOA · Seen Mar 2026
- Social Media SuggestionsOA · Seen May 2025
- Best Sum Downward Tree PathOA · Seen May 2025