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:
-1
💡 Note:
Day 1: bulb 1 on. Day 2: bulb 5 on, bulbs 1 and 5 have 3 positions between them (not k=2). Day 3: bulb 3 on, but no two lit bulbs have exactly k=2 positions between them. Return -1.
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code