BFS traversal dynamically discovered graph
IntuitionBFS traversal dynamically discovered graph.ApproachI was really stuck withcomputing all possible eligible nodes for the jump on every stage. It was too
# Intuition
BFS traversal dynamically discovered graph.

# Approach
I was really stuck withcomputing all possible eligible nodes for the jump on every stage. It was too slow.
I had too give up on memory efficiency. I added separate index for banned, and sorted sets for all possible odd and even targets. Sorted sets are particulary good a getting slices and removing ranges. This allowed to save time for pass the submission.

# Complexity
- Time complexity:$$O(n^2)$$ max, should be close to $$O(n log n)$$ on average unless p close to the edges which effectively makes BFS $$O(V+E)$$ where $$E=V^2$$

- Space complexity: $$O(n)$$

# Code
```csharp []
public class Solution {


    public int[] MinReverseOperations(int n, int p, int[] banned, int k) {

        // Initialize all results to -1.
        int[] result = new int[n];
        Array.Fill(result, -1);

        // Mark banned indices.
        bool[] bannedArr = new bool[n];
        foreach (int b in banned)
            bannedArr[b] = true;

        // Use two sets for unvisited, non-banned indices: one for even and one for odd.
        SortedSet<int> evenSet = new SortedSet<int>();
        SortedSet<int> oddSet = new SortedSet<int>();
        for (int i = 0; i < n; i++)
        {
            if (i == p || bannedArr[i])
                continue;
            if ((i & 1) == 0)
                evenSet.Add(i);
            else
                oddSet.Add(i);
        }

        // Standard BFS.
        Queue<int> queue = new Queue<int>();
        queue.Enqueue(p);
        result[p] = 0;
        int step = 0;
        while (queue.Count > 0)
        {
            step++;
            int levelSize = queue.Count;
            for (int q = 0; q < levelSize; q++)
            {
                int cur = queue.Dequeue();

                // Compute the valid range for l (the starting index of the reversal segment)
                int L = Math.Max(0, cur - k + 1);
                int R = Math.Min(cur, n - k);
                if (L > R)
                    continue; // No valid reversal operation possible

                // The new index after reversal is: j = 2 * l + k - 1 - cur.
                // Thus, as l runs from L to R, j runs from:
                int low = 2 * L + k - 1 - cur;
                int high = 2 * R + k - 1 - cur;

                // The parity of j is fixed: it is the same as (k - 1 - cur) mod 2.
                SortedSet<int> targetSet = (((k - 1 - cur) & 1) == 0) ? evenSet : oddSet;

                // Get all available indices in targetSet within [low, high].
                // ToList() is used because we'll modify the set while iterating.
                var candidates = targetSet.GetViewBetween(low, high).ToList();
                foreach (int cand in candidates)
                {
                    result[cand] = step;
                    queue.Enqueue(cand);
                    targetSet.Remove(cand);
                }
            }
        }

        return result;
    }
}
```
['C#']