Ctrl K

A Dark Gap Between Street Lamps

Hard
A street has n lamps numbered 1 to n, all off at first. Exactly one lamp is switched on each day: on day i + 1 it is lamp number order[i]. After n days every lamp is on. Find the earliest day after which there are two lit lamps with exactly gap lamps between them, all of them still off. Return that day (days are numbered from 1), or -1 if no such day exists.
Example 1
order=[2, 5, 4, 1, 3], gap=2 → 2

After day 2 lamps 2 and 5 are on, and lamps 3 and 4 between them are still dark: exactly two. The answer is 2.

Example 2
order=[3, 1, 2], gap=1 → 2

On day 2 lamps 1 and 3 are on with the single dark lamp 2 between them.

Example 3
order=[1, 2, 3, 4], gap=1 → -1

Lamps light up one after another, so two lit lamps never have a dark one between them. The answer is -1.

Constraints
  • 1 ≤ n = len(order) ≤ 2 · 10⁴
  • order is a permutation of 1 to n
  • 0 ≤ gap ≤ 2 · 10⁴
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Tight-Range Stretch MediumSteady Stretches of Readings MediumCity Block Outline HardMoving Window Maximum HardShortest Stretch Reaching a Total Hard
loading Python…
Input
order=[2, 5, 4, 1, 3], gap=2
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 117 tests on the server, hidden ones included.