An array of integers. It is guaranteed that all nums from 1 to nums.length appear exactly once in the array.
Problem
Return such t (0 <= t < nums.length) that cyclic t-shift operation turns nums into a reverse sorted array [n, n - 1, ..., 1]. If it is not possible to turn nums into reverse sorted array by performing a cyclic t-shift, return -1.
Constraints
- 3 < nums.length < 100
- 1 <= nums[i] <= nums.length
Example
- For nums = [1, 4, 2, 3], the output should be solution(nums) = -1.
Let's consider all possible cyclic t-shifts:
- cyclic 0-shift: moving 0 elements from the end to the beginning, we get [1, 4, 2, 3].
- cyclic 1-shift: moving 1 element from the end to the beginning, we get [3, 1, 4, 2].
- cyclic 2-shift: moving 2 elements from the end to the beginning, we get [2, 3, 1, 4].
- cyclic 3-shift: moving 3 elements from the end to the beginning, we get [4, 2, 3, 1].
None of the resulting arrays equals [4, 3, 2, 1], so the answer is -1.
- For nums = [3, 2, 1, 5, 4], the output should be solution(nums) = 2.
If we move the last 2 elements of the given array from the end to the beginning, we get [5, 4, 3, 2, 1], so the answer is 2.