Java Solution With Detailed Explanation
Background\n\nSuppose the value 1 in the array is at index x. Let\u2019s think of the places we could move it to if we could reverse any subarray of size k cont
# Background\n\nSuppose the value 1 in the array is at index x. Let\u2019s think of the places we could move it to if we could reverse any subarray of size k containing x. If we have an array of size 10 and x is 6 and k is 4, we could move x to positions   3,5,7 and 9. If k is 3, we could move x to 4, 6(current position), 8. Notice that the parity (mod 2) of index we could move to are same. So if we can find the minimum and maximum index that could be visited, we can assume that all index of same parity in between can be visited with a single reversal.\n\n# Intuition\n\u2028To solve this problem, we can perform a bfs starting at p. We also use two TreeSet (odd and even parity index) to keep track of the index which are not banned and haven\u2019t been visited. While performing the bfs, we find the parity of index we can move to if we  reversing a subarray k containing the index x. Next we find the minimum and maximum index we can move to with a single reversal. In most cases, the minimum index is x - k + 1 and the maximum is x + k - 1. However there are special cases where x is closer to the extreme of the array. If x is closer to the left of the array more formally x < k, then the minimum (k - 1) - x. A Similar logic also apply to the maximum when x closer to the right end. Once we have the parity, max and min index we could move to from the current index under consideration. We can add all values of index in the treeset with the right parity to the queue and remove the values from the set since they are already visited. This process would be repeated until the queue is empty as in the standard bfs.\n\nTime Complexity : O(n log n) as treeset operations take O(log n)\nSpace Complexity: O(n) used by the sets\n\nPlease upvote if you like the solution :)\n\n# Code\n\n```\nclass Solution {\n    public int[] minReverseOperations(int n, int p, int[] banned, int k) {\n        TreeSet<Integer> even = new TreeSet<>();\n        TreeSet<Integer> odd = new TreeSet<>();\n        \n        TreeSet<Integer> set;\n        \n        boolean [] skip = new boolean [n];\n        \n        for (int num : banned) {\n            skip[num] = true;\n        }\n        \n        int start = p;\n        \n        // Add values to the set that are not banned\n        for (int i = 0; i < n; ++i) {\n            if (skip[i] || i == p)\n                continue;\n            \n            if (i % 2 == 1)\n                odd.add(i);\n            else\n                even.add(i);\n        }\n        \n        int [] result = new int [n];\n        Arrays.fill(result, -1);\n        \n        Queue<Integer> queue = new LinkedList<>();\n        queue.add(p);\n        \n        int size;\n        int current;\n        int moves = 0;\n        \n        int min, max;\n        \n        Integer key;\n        \n        int mCurrent;\n        \n        while (!queue.isEmpty()) {\n            size = queue.size();\n            \n            \n            while (size-- > 0) {\n                current = queue.remove();\n                \n                result[current] = moves;\n                \n                \n                // calculate min index\n                if (current < k - 1) {\n                    min = (k - 1) - current;\n                }else {\n                    min = current - k + 1;\n                }\n                \n                // calculate max index\n                mCurrent = (n - 1) - current;\n                if (mCurrent < k - 1) {\n                    max = (k - 1) - mCurrent;\n                }else {\n                    max = mCurrent - k + 1;\n                }\n                max = (n - 1) - max;\n                \n                \n                // chose the correct parity set\n                set = min % 2 == 0 ? even : odd;\n                \n                key = set.ceiling(min);\n                \n                // add all values in range to the queue and remove from set\n                while (key != null && key <= max) {\n                    queue.add(key);\n                    set.remove(key);\n                    key = set.ceiling(min);\n                }\n            }\n            \n            ++moves;\n        }\n        \n        \n        return result;\n    }\n}\n```
['Java']