Skip to main content

K Empty Slots - Solution & Explanation

HardPremiumFree on FleetCodeArrayBinary Indexed TreeSegment TreeQueue8 min readAsked at: Google
Practice this problem

Problem Statement

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.

 

Example 1:

Input: bulbs = [1,3,2], k = 1
Output: 2
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.

Example 2:

Input: bulbs = [1,2,3], k = 1
Output: -1

 

Constraints:

  • n == bulbs.length
  • 1 <= n <= 2 * 104
  • 1 <= bulbs[i] <= n
  • bulbs is a permutation of numbers from 1 to n.
  • 0 <= k <= 2 * 104

Approach Overview

Problem Overview: You receive an array bulbs where bulbs[i] tells which position blooms on day i + 1. The goal is to find the earliest day when two blooming flowers have exactly k unbloomed slots between them.

Approach 1: Brute Force Simulation (O(n * k) time, O(n) space)

Track which flowers have bloomed using a boolean array. After each day, check the newly bloomed position and look left and right to see if another bloomed flower exists exactly k + 1 distance away. Then verify the k slots between them remain unbloomed. This method directly simulates the condition but repeatedly scans segments of size k, making it inefficient for large inputs.

Approach 2: Binary Indexed Tree / Fenwick Tree (O(n log n) time, O(n) space)

Maintain a Fenwick Tree to track how many flowers have bloomed up to a given index. When a flower blooms at position p, query whether a flower exists at p - k - 1 or p + k + 1. Then use the Fenwick Tree to count how many flowers exist inside the interval between them. If the count inside the gap is zero, the condition is satisfied. Each update and range query runs in O(log n). This approach is reliable when you need fast prefix counts over dynamically updated positions, which is exactly what Binary Indexed Tree structures handle well.

Approach 3: Sliding Window with Bloom Days (O(n) time, O(n) space)

Create an array days where days[i] represents the day flower i blooms. Use a window of size k + 2 with two pointers representing the potential boundary flowers. For a valid window, every flower between them must bloom later than both boundaries. If an interior flower blooms earlier, the window shifts starting from that position. This method works because the earliest boundary bloom day determines when the condition is satisfied. It uses a technique similar to Sliding Window and can be optimized using ideas related to a Monotonic Queue.

Recommended for interviews: The sliding window approach is the expected optimal solution because it achieves O(n) time by preprocessing bloom days and scanning once. The Binary Indexed Tree solution is also strong and demonstrates knowledge of advanced data structures for dynamic range queries. Showing the brute force idea first demonstrates understanding of the problem constraints before moving to the optimized strategy.

Solution

We can use a Binary Indexed Tree to maintain the prefix sum of the bulbs. Every time we turn on a bulb, we update the corresponding position in the Binary Indexed Tree. Then we check if the k bulbs to the left or right of the current bulb are all turned off and the (k+1)-th bulb is already turned on. If either of these conditions is met, we return the current day.

The time complexity is O(n times log n) and the space complexity is O(n), where n is the number of bulbs.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force SimulationO(n * k)O(n)Useful for understanding the condition and verifying logic on small inputs
Binary Indexed Tree (Fenwick Tree)O(n log n)O(n)When dynamic prefix counts or range queries are required
Sliding Window on Bloom DaysO(n)O(n)Optimal solution for interviews and large inputs

Video Solution

花花酱 LeetCode 683. K Empty Slots - 刷题找工作 EP76 • Hua Hua • 9,418 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is K Empty Slots easy or hard?
K Empty Slots is classified as a hard problem because it requires transforming the problem into bloom days and applying a non-obvious sliding window technique. Many initial solutions are O(n^2) or O(nk) before discovering the linear-time approach.
K Empty Slots Python/Java solution
Python and Java implementations usually follow either the O(n) sliding window method or an O(n log n) Binary Indexed Tree approach. The sliding window version is shorter and preferred in interviews, while the Fenwick Tree version highlights range query data structure knowledge.
How to solve K Empty Slots in O(n)?
First convert the input into a days array where days[i] represents the day the flower at position i blooms. Use two pointers representing a window of size k + 2. If any flower inside the window blooms earlier than the boundaries, shift the window; otherwise record the candidate day.
What is the best approach for K Empty Slots?
The optimal approach uses a sliding window over a precomputed bloom-day array and runs in O(n) time with O(n) space. Each window checks whether all flowers inside bloom later than the two boundary flowers. If the condition holds, the later boundary bloom day gives the answer.
Is K Empty Slots asked at Google/Amazon/Meta?
K Empty Slots is a known hard interview problem and has appeared in interviews at companies like Google and Amazon. It tests understanding of sliding window techniques, ordered structures, and interval validation logic.
What data structure is used in K Empty Slots?
Common solutions use arrays with a sliding window, Binary Indexed Tree (Fenwick Tree), Segment Tree, or ordered sets. These structures help track which flowers have bloomed and quickly check whether the gap between two flowers is empty.
What is the time complexity of K Empty Slots?
The best known solution runs in O(n) time using a sliding window over bloom days. A Binary Indexed Tree or Segment Tree solution runs in O(n log n) because each bloom requires a logarithmic update and query.

Ready to solve this problem?

Practice K Empty Slots with our built-in code editor and test cases.

Practice on FleetCode