Minimum Reverse Operations - Problem
You are given an integer n and an integer p representing an array arr of length n where all elements are set to 0's, except position p which is set to 1.
You are also given an integer array banned containing restricted positions and an integer k representing the size of subarrays you can reverse.
Operation: Reverse a subarray of size k if the single 1 is not at a position in banned.
Return an integer array answer with n results where the ith result is the minimum number of operations needed to bring the single 1 to position i in arr, or -1 if it is impossible.
Input & Output
Example 1 — Basic Case
$
Input:
n = 4, p = 0, banned = [1,2], k = 4
›
Output:
[0,-1,-1,1]
💡 Note:
Start at position 0. Position 1 and 2 are banned (-1). To reach position 3: reverse entire array [0,1,2,3] with k=4, moving element from position 0 to position 3 in 1 step.
Example 2 — No Banned Positions
$
Input:
n = 3, p = 0, banned = [], k = 3
›
Output:
[0,2,1]
💡 Note:
Start at 0. Reverse [0,1,2] moves 0→2 (1 step). From 2, reverse [0,1,2] moves 2→0, then reverse moves 0→2→1 (2 steps total to reach 1).
Example 3 — Impossible Case
$
Input:
n = 2, p = 0, banned = [], k = 2
›
Output:
[0,1]
💡 Note:
Start at 0. Reverse [0,1] with k=2 swaps positions, so 0→1 in 1 step. Both positions reachable.
Constraints
- 1 ≤ n ≤ 105
- 0 ≤ p < n
- 0 ≤ banned.length ≤ n - 1
- 0 ≤ banned[i] < n
- banned[i] ≠ p
- 2 ≤ k ≤ n
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with single 1 at position p, some banned positions
2
Process
Use k-reversals to move the 1 to different positions
3
Output
Minimum steps to reach each position or -1 if impossible
Key Takeaway
🎯 Key Insight: Model as shortest path problem using BFS to find minimum operations to each position
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code