Sorting Array by Reversing Around the Middle is a greedy/symmetry puzzle. The only operation allowed is reversing a chunk around the center of the array, and the question is whether such reversals can ever leave the array sorted. The whole problem collapses to a simple symmetry check.
Problem. You may reverse any subarray that is symmetric about the center of a (it swaps mirror positions: index i with index n-1-i). Decide whether a can be made sorted using such reversals.
Example: a = [1, 4, 3, 4, 1] → answer True (every mirror pair already matches: a[0]==a[4], a[1]==a[3], and the middle is alone).
The slow way first
You could try to simulate reversals — pick a center-symmetric window, reverse it, and search for a sequence that ends sorted. But the number of reversal sequences explodes, and most lead nowhere. Brute-force search over operations is exponential and tells you nothing about why an array works.
The question to ask: what does a center reversal actually preserve? Reversing around the middle only ever swaps an element at position i with its mirror at n-1-i. It never breaks a pair apart — it just exchanges the two members of a mirror pair.
The idea: check the mirror pairs
Because every allowed move only swaps i with n-1-i, the multiset of each mirror pair never changes. For the final array to be sorted (and for these moves to even be capable of producing it), the two members of each mirror pair must be equal — otherwise the pair is stuck holding two different values that the operation can only swap, never fix.
So the answer is: walk a pointer i in from the left and j in from the right. If a[i] != a[j] for any pair, return False. If all pairs match, return True.
The key insight: a center reversal is just a mirror swap, so sortability reduces to "is the array already mirror-symmetric in value?"
Walk through it
Step through the animation. Pointer i starts at the far left, j at the far right, and they close toward the center. Each step compares one mirror pair. Pairs (0,4) and (1,3) both match, the pointers meet at the lone middle element, and we return True.
Pseudocode
i = 0, j = last index
while i < j:
if a[i] != a[j]:
return False # this mirror pair can never be fixed
i = i + 1
j = j - 1
return True # every mirror pair matchedThe Python solution
def can_sort_by_center_reversal(a):
i, j = 0, len(a) - 1
while i < j:
if a[i] != a[j]:
return False
i += 1
j -= 1
return Truei, j = 0, len(a) - 1sets the two pointers at the ends.while i < jruns until they cross or meet — a lone middle element needs no check.- Line 4 is the heart:
a[i] != a[j]tests one mirror pair. A single mismatch kills it. i += 1; j -= 1steps both pointers inward by one.- If we exit the loop with no mismatch, every mirror pair matched, so we
return True.
Complexity
| Case | Time | Notes |
|---|---|---|
| Simulating reversals | exponential (moderate) | search over operations |
| Mirror-pair check (this solution) | O(n) (moderate) | one inward pass |
O(1) (fast)We touch each element at most once and store only two indices, so it is O(n) time and O(1) space.
When this pattern shows up
When an operation is restricted (reverse, rotate, swap-in-place), ask what it preserves — an invariant. Center reversals preserve mirror pairs, so the answer is just a two-pointer symmetry scan. Spotting the invariant turns an exponential simulation into a one-line check.
Stop the loop at i < j, not i <= j. For odd-length arrays the middle element is its own mirror and
comparing it to itself is harmless, but for even lengths i <= j would re-compare a pair after they cross.
Practice
For a = [1, 4, 3, 4, 1], what mirror pairs does the algorithm compare, and do they all match?
1. Why does the problem reduce to checking a[i] == a[n-1-i] for all pairs?
2. What does the algorithm return the moment it finds a[i] != a[j]?
3. What is the time and space complexity of the two-pointer check?
4. Why does the loop stop at i < j rather than i <= j?