Problem Description
Given an array of distinct positive integers, determine the minimum number of right shifts required to sort the array in ascending order. A right shift means that every element moves one position to the right and the last element moves to the front. If it is impossible to sort the array using right shifts, return -1.
Key Insights
- A sorted array that is rotated will have at most one "drop" (an index i where nums[i] > nums[i+1]).
- If the array is already sorted (no drops), no shifts are required.
- If there is one drop, the number of shifts needed is n - (drop index + 1), provided that the last element is less than or equal to the first element.
- If there is more than one drop, the array cannot be sorted by cyclic right shifts, hence return -1.
Space and Time Complexity
Time Complexity: O(n) because we scan the array once.
Space Complexity: O(1) as only a few variables are used.
Solution
The solution involves scanning the array once to identify if it can be treated as a rotated version of a sorted array. Count the number of drops (where an element is greater than its next neighbor).
- If no drops are found, the array is already sorted and zero shifts are needed.
- If exactly one drop is found, then calculate the number of shifts as (n - drop_index - 1). However, a final check is necessary: ensure that the last element is not greater than the first element; otherwise, a valid sorted rotation is not possible.
- If more than one drop is identified, then sorting by right shifts is impossible, so return -1.
The algorithm relies on simple linear traversal and arithmetic operations, making it both time and space efficient.