K Empty Slots - Problem
You have n bulbs in a row numbered from 1 to n. Initially, all the bulbs are turned off. We turn on exactly one bulb every day until all bulbs are on after n days.
You are given an array bulbs of length n where bulbs[i] = x means that on the (i+1)th day, we will turn on the bulb at position x where i is 0-indexed and x is 1-indexed.
Given an integer k, return the minimum day number such that there exists two turned on bulbs that have exactly k bulbs between them that are all turned off. If there isn't such day, return -1.
Input & Output
Example 1 — Basic Case
$
Input:
bulbs = [1,3,2], k = 1
›
Output:
2
💡 Note:
Day 1: bulb 1 is on. Day 2: bulb 3 is on, now we have bulbs 1 and 3 with exactly 1 bulb (position 2) between them that is off. Return day 2.
Example 2 — No Valid Pattern
$
Input:
bulbs = [1,2,3], k = 1
›
Output:
-1
💡 Note:
Day 1: bulb 1 on. Day 2: bulbs 1,2 on (no gap). Day 3: bulbs 1,2,3 on (no valid k=1 gap). Return -1.
Example 3 — Larger Gap
$
Input:
bulbs = [1,5,3], k = 2
›
Output:
3
💡 Note:
Day 1: bulb 1 on. Day 2: bulbs 1,5 on (3 bulbs between). Day 3: bulb 3 on, now bulbs 1,3 have exactly k=2 bulbs between (positions 2,4). Return day 3.
Constraints
- 1 ≤ bulbs.length ≤ 2 × 104
- 1 ≤ bulbs[i] ≤ bulbs.length
- bulbs is a permutation of numbers from 1 to bulbs.length
- 0 ≤ k ≤ 2 × 104
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of bulb positions and target gap k
2
Process
Turn on bulbs day by day, check for k-gaps
3
Output
First day when valid k-gap pattern exists
Key Takeaway
🎯 Key Insight: Only check newly lit bulbs against their immediate neighbors, as they form the smallest possible valid windows.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code