Ctrl K

Last Step With a Lit Run

Medium
A row of n lamps, numbered 1 to n, starts with every lamp off. The array order is a permutation of 1 to n: on step i (1-based) lamp order[i - 1] is switched on and stays on. A run is a maximal stretch of consecutive lit lamps, meaning the lamps immediately to its left and right (if any) are off. Return the last step after which the row contains at least one run of length exactly size. If there is no such step, return -1.
Example 1
order=[2, 4, 1, 3], size=2 → 3

States: 0100, 0101, 1101, 1111. After step 3 there is a run 11 of length 2; after step 4 everything merges into one run of length 4. The answer is 3.

Example 2
order=[1, 3, 2], size=2 → -1

States: 100, 101, 111. A run of length exactly 2 never appears, so the answer is -1.

Example 3
order=[3, 1, 2], size=3 → 3
Constraints
  • n = len(order)
  • 1 ≤ size ≤ n ≤ 10⁵
  • order is a permutation of 1, 2, …, n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPairs With a Fixed Gap MediumWords Hidden in a Text MediumHow Far the Rover Got MediumLongest Equal-Gap Chain MediumWeight by the Earliest Letter Medium
loading Python…
Input
order=[2, 4, 1, 3], size=2
Expected
3

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