Given a permutation of length $$$n \geq 2$$$, what is the maximum number of fixed points after reversing a subarray exactly once? Subarray should have length $$$\geq 2$$$. A fixed point is $$$a_i = i$$$.
Input:
7
6 2 3 4 5 1 7
Output:
4
$$$2, 3, 4, 5$$$ are already fixed points we can reverse last $$$2$$$ elements.
I can't solve it faster than $$$O(n^3)$$$, which will try every possible subarray. Any help to find an $$$O(n)$$$ solution? This problem came from random thoughts it's not from an ongoing contest.