UberOA #54
Minimum Right Shifts to Sort the Array
Estimated EasyJudge-ready
Topics (AI-labelled)In-place Arrays
The array contains distinct positive integers. Each operation moves the last element to the front and shifts the other elements one position right. Find the minimum number of operations to make the array strictly increasing, or −1 if impossible.
This site has made the standard input and output explicit.
Input
The first line contains n, and the second line contains n distinct integers; 1≤n≤100, 1≤a[i]≤100.
Output
Output one integer, the answer.
Example 1
Input:
5
3 4 5 1 2
Output:
2
Example 2
Input:
3
1 3 5
Output:
0
Example 3
Input:
3
2 1 4
Output:
-1
Example notes
Example 1: two consecutive right shifts give 1, 2, 3, 4, 5, so the answer is 2. Example 2: already sorted, so the answer is 0. Example 3: the cycle has two descending edges, 2→1 and 4→2, so rotation alone cannot sort it; output −1.
Editorial
Loading solution…