Minimum Reverse Operations

You are given an integer n and an integer p representing an array arr of length n where all elements are set to 0, except position p which is set to 1. You are also given an integer array banned containing restricted positions.

Perform the following operation on arr:

  • Reverse a subarray with size k if the single 1 is not set to a position in banned.

Return an integer array answer with n results where the i^th result is the minimum number of operations needed to bring the single 1 to position i in arr, or -1 if it is impossible.

Example 1
Inputn = 4, p = 0, banned = [1,2], k = 4
Output[0,-1,-1,1]
Initially position 0 needs 0 operations, positions 1 and 2 are banned, and reversing the whole array moves the 1 to position 3 in one operation.
Example 2
Inputn = 5, p = 0, banned = [2,4], k = 3
Output[0,-1,-1,-1,-1]
The 1 starts at position 0, but the only useful size-3 reversal would include banned position 2, so no other position can be reached.

Constraints

  • 1 <= n <= 10^5
  • 0 <= p <= n - 1
  • 0 <= banned.length <= n - 1
  • 0 <= banned[i] <= n - 1
  • 1 <= k <= n
  • banned[i] != p
  • all values in banned are unique

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate