💥💥 Beats 100% on memory and runtime [EXPLAINED]
IntuitionThe main idea is to calculate the shortest distance from the starting position p to all other indices, using a Breadth-First Search (BFS). Since we are
# Intuition
The main idea is to calculate the shortest distance from the starting position p to all other indices, using a Breadth-First Search (BFS). Since we are allowed to reverse a segment of the array, and we can only visit every second element (based on the given k), we need a way to efficiently skip already visited indices and explore only valid positions. We use Disjoint Set Union (DSU) to manage and optimize which elements we’ve already visited by connecting each index to the next unvisited one.

# Approach
We initialize two arrays, even and odd, to track the parent of each index (using DSU). We perform BFS from the starting position p, calculate the left and right boundaries of the sliding window (left, right) considering k, and adjust them if necessary. For each valid index, we update its parent to skip it in future iterations. BFS propagates the minimum distance to all reachable indices in the array.

# Complexity
- Time complexity:
O(n) because we process each index once, and the DSU operations (find and union) are almost constant time, thanks to path compression.

- Space complexity:
O(n) due to the storage needed for the even and odd arrays, which track the parent relationships for all indices.

# Code
```rust []
use std::collections::VecDeque;

impl Solution {
    // Helper function to find the parent of an index in the even array
    fn even_par(i: i32, even: &mut Vec<i32>) -> i32 {
        // If the index is its own parent, return it
        if i == even[i as usize] {
            i
        } else {
            // Otherwise, recursively find the parent and update the parent array
            even[i as usize] = Solution::even_par(even[i as usize], even);
            even[i as usize]
        }
    }

    // Helper function to find the parent of an index in the odd array
    fn odd_par(i: i32, odd: &mut Vec<i32>) -> i32 {
        // If the index is its own parent, return it
        if i == odd[i as usize] {
            i
        } else {
            // Otherwise, recursively find the parent and update the parent array
            odd[i as usize] = Solution::odd_par(odd[i as usize], odd);
            odd[i as usize]
        }
    }

    pub fn min_reverse_operations(n: i32, p: i32, banned: Vec<i32>, k: i32) -> Vec<i32> {
        let mut ans = vec![-1; n as usize];  // Initialize result vector, all distances set to -1

        // Initialize the parent arrays for even and odd indices
        let mut even = (0..=n + 1).collect::<Vec<i32>>();  // +2 for boundary cases (to handle n+1)
        let mut odd = (0..=n + 1).collect::<Vec<i32>>();   // +2 for boundary cases (to handle n+1)

        // Mark banned indices by updating their parents (+2 distance)
        for &i in &banned {
            if i % 2 == 1 {  // If the index is odd
                odd[i as usize] = i + 2;  // Set its parent to be the next odd index (+2)
            } else {  // If the index is even
                even[i as usize] = i + 2;  // Set its parent to be the next even index (+2)
            }
        }

        // Adjust the starting position 'p', based on whether it's even or odd
        if p % 2 == 1 {
            odd[p as usize] = p + 2;  // If it's odd, set the parent of p to p + 2
        } else {
            even[p as usize] = p + 2;  // If it's even, set the parent of p to p + 2
        }

        // BFS initialization: Distance from p is 0, start BFS from p
        let mut dis = 0;  // Distance from the source (p)
        ans[p as usize] = 0;  // Starting point has a distance of 0
        let mut queue = VecDeque::new();
        queue.push_back(p);  // Add starting point to the BFS queue

        // BFS loop: Explore each level of the graph
        while !queue.is_empty() {
            dis += 1;  // Increment the distance for each level
            let sz = queue.len();  // Get the number of nodes to explore at this level

            // Explore all nodes at the current BFS level
            for _ in 0..sz {
                let curr = queue.pop_front().unwrap();  // Dequeue the next node

                // Calculate the left and right boundaries of the sliding window
                let mut left = curr - k + 1;  // Left boundary considering k
                if left < 0 {
                    left = k - curr - 1;  // Adjust left boundary if it goes out of range
                }

                let mut right = curr + k - 1;  // Right boundary considering k
                if right >= n {
                    right = n + n - k - curr - 1;  // Adjust right boundary if it goes out of range
                }

                // Handle traversal based on the parity (odd or even) of the left boundary
                if left % 2 == 1 {  // If left boundary is odd
                    let mut i = Solution::odd_par(left, &mut odd);  // Get the parent of the left boundary in odd array
                    while i <= right {  // Traverse the range from left to right
                        ans[i as usize] = dis;  // Set the distance for this index
                        odd[i as usize] = i + 2;  // Skip to the next unvisited index (+2)
                        queue.push_back(i);  // Add this index to the BFS queue
                        i = Solution::odd_par(i, &mut odd);  // Get the next parent in the odd array
                    }
                } else {  // If left boundary is even
                    let mut i = Solution::even_par(left, &mut even);  // Get the parent of the left boundary in even array
                    while i <= right {  // Traverse the range from left to right
                        ans[i as usize] = dis;  // Set the distance for this index
                        even[i as usize] = i + 2;  // Skip to the next unvisited index (+2)
                        queue.push_back(i);  // Add this index to the BFS queue
                        i = Solution::even_par(i, &mut even);  // Get the next parent in the even array
                    }
                }
            }
        }

        ans  // Return the final result vector with the distances
    }
}

```
['Array', 'Binary Search', 'Dynamic Programming', 'Bit Manipulation', 'Breadth-First Search', 'Graph', 'Binary Search Tree', 'Ordered Set', 'Rust']