K Empty Slots is a fascinating problem about timing and patterns in sequential events.

Imagine you have n light bulbs arranged in a row, numbered from 1 to n. Initially, all bulbs are off. Each day, you turn on exactly one bulb according to a predetermined schedule given in array bulbs.

The array bulbs[i] = x means that on day i+1, you'll turn on the bulb at position x. Your goal is to find the earliest day when there exist two turned-on bulbs with exactly k bulbs between them that are all turned off.

For example, if k=2, you're looking for a pattern like: [ON, off, off, ON] where positions 1 and 4 are on, but positions 2 and 3 are off.

Return the minimum day number when this pattern first appears, or -1 if it never happens.

Input & Output

example_1.py — Basic case with k=1
$ Input: bulbs = [1,3,2], k = 1
› Output: 2
šŸ’” Note: On day 1, bulb 1 is on. On day 2, bulb 3 is on. Now we have pattern [ON, off, ON] where positions 1 and 3 have exactly 1 bulb (position 2) between them that is off. So answer is day 2.
example_2.py — No valid pattern
$ Input: bulbs = [1,2,3], k = 1
› Output: -1
šŸ’” Note: Bulbs turn on in order 1→2→3. We never get a pattern where two on bulbs have exactly 1 off bulb between them, since adjacent bulbs turn on consecutively.
example_3.py — Larger gap
$ Input: bulbs = [6,5,8,4,7,10,9], k = 2
› Output: 3
šŸ’” Note: We need pattern [ON, off, off, ON]. On day 3, bulb 8 turns on. We then have bulbs 6 and 8 on with exactly 2 bulbs (position 7) between them that are off.

Visualization

Tap to expand
Street Light Installation TimelineDay 1Day 4Day 5Day 2Day 6Day 3Day 7Safe Zone: k=2 gapsPattern found on Day 2: Lights at positions 1&4 with exactly 2 dark spots betweenSliding Window Algorithm:1. Map each light to its installation day2. Check windows of size k+23. Verify gap lights turn on after boundaries4. Return earliest valid pattern dayšŸŽÆ Time Complexity: O(n) - Each position checked once!
Understanding the Visualization
1
Daily Installation
Each day, one more street light is turned on according to schedule
2
Pattern Recognition
Look for two lit areas with exactly k dark areas between them
3
Timing Check
Ensure the gap stays dark until both boundary lights are on
4
Safety Achievement
Report the first day this safety pattern is achieved
Key Takeaway
šŸŽÆ Key Insight: Instead of checking all pairs daily (O(n³)), we precompute installation days and use sliding windows to check patterns in O(n) time by ensuring gap bulbs turn on after boundary bulbs.

Time & Space Complexity

Time Complexity
ā±ļø
O(n³)

For each of n days, we check O(n²) pairs, and for each pair we check O(n) bulbs between them

n
2n
⚠ Quadratic Growth
Space Complexity
O(n)

Need array to track which bulbs are lit

n
2n
⚔ Linearithmic Space

Constraints

  • 1 ≤ bulbs.length ≤ 2 Ɨ 104
  • 1 ≤ bulbs[i] ≤ bulbs.length
  • bulbs is a permutation of numbers from 1 to bulbs.length
  • 1 ≤ k ≤ 2 Ɨ 104
  • All bulb positions are distinct
Asked in
Google 45 Facebook 32 Amazon 28 Microsoft 15
23.4K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
šŸ’” Explanation
AI Ready
šŸ’” Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen