LeetCode 683, Hard. Topics: Array, Binary Indexed Tree, Segment Tree, Queue, Ordered Set, Sliding Window, Monotonic Queue, Heap (Priority Queue). View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 41 parametrized pytest cases, and a playground notebook:
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 is no such day, return -1.
Examples
Explanation:
- On the first day: bulbs[0] = 1, first bulb is turned on: [1,0,0]
- On the second day: bulbs[1] = 3, third bulb is turned on: [1,0,1]
- On the third day: bulbs[2] = 2, second bulb is turned on: [1,1,1]
We return 2 because on the second day, there were two on bulbs with one off bulb between them.
Constraints
n == bulbs.length
1 <= n <= 2 * 10^4
1 <= bulbs[i] <= n
bulbs is a permutation of numbers from 1 to n.
0 <= k <= 2 * 10^4
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
Last modified on September 7, 2026