Problem · Array
Minimum Right Shifts to Sort the Array
Learn this problemProblem statement
You are given a 0-indexed array nums of length n containing distinct positive integers. Return the minimum number of right shifts required to sort nums and -1 if this is not possible.
A right shift is defined as shifting the element at index i to index (i + 1 % n), for all indices.
Function
minimumRightShiftsToSortArray(nums: int[]) → int
Complete the function minimumRightShiftsToSortArray in the editor.
minimumRightShiftsToSortArray has the following parameter:
int[] nums: a 0-indexed array containing distinct positive integers
Returns
int: the minimum number of right shifts required to sort nums, or -1 if it is not possible
Examples
Example 1
nums = [3,4,5,1,2]return = 2After the first right shift, nums = [2,3,4,5,1].
After the second right shift, nums = [1,2,3,4,5].
Now nums is sorted; therefore the answer is 2.
Example 2
nums = [1,3,5]return = 0nums is already sorted therefore, the answer is 0.
Example 3
nums = [2,1,4]return = -1It's impossible to sort the array using right shifts.
Constraints
1 <= nums.length <= 1001 <= nums[i] <= 100numscontains distinct integers.
More Uber problems
- Last Truck to Leave the LaneOA · Seen Jul 2026
- Chain of CommandOA · Seen Jul 2026
- Jump Game with Prime-3 StepsOA · Seen Jun 2026
- Total Palindrome Substring CostOA · Seen Jun 2026
- Earliest Time All Users Are ConnectedPHONE SCREEN · Seen May 2026
- Tournament Rounds by RankPHONE SCREEN · Seen May 2026
- Farthest Seat AssignmentONSITE INTERVIEW · Seen May 2026
- Convex Function MinimizationPHONE SCREEN · Seen May 2026