You are given an integer array nums of length n, where nums is a permutation of the integers from 0 to n - 1. You may perform only the following operations: - Reverse the entire array. - Rotate Left by One: Move the first element to the end of the array, and the rest of the elements left by one position. Return an integer denoting the minimum number of operations required to sort the array in increasing order. If it is not possible to sort the array using only the given operations, return -1.
Example 1
Input: nums = [0,2,1]
Output: 2
Explanation: Rotate left by one to get [2,1,0], then reverse to get [0,1,2]. Two operations sort it, and no single operation can.
Example 2
Input: nums = [1,0,2]
Output: 2
Explanation: Reverse to get [2,0,1], then rotate left by one to get [0,1,2]. Two operations are the minimum.
Example 3
Input: nums = [2,0,1,3]
Output: -1
Explanation: This arrangement is neither a rotation of the increasing order nor of the decreasing order, so reverse and rotate-left can never sort it.
The two operations can only ever produce a rotation of nums or a rotation of its reverse, so there are at most 2n reachable arrangements — compress each into (type, shift) and BFS from nums to the sorted arrangement.
Run checks all cases above. Submit evaluates all test cases.