2855. Minimum Right Shifts to Sort the Array LeetCode Solution
In this guide, you will get 2855. Minimum Right Shifts to Sort the Array LeetCode Solution with the best time and space complexity. The solution to Minimum Right Shifts to Sort the Array problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Right Shifts to Sort the Array solution in C++
- Minimum Right Shifts to Sort the Array solution in Java
- Minimum Right Shifts to Sort the Array solution in Python
- Additional Resources

Problem Statement of Minimum Right Shifts to Sort the Array
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.
Example 1:
Input: nums = [3,4,5,1,2]
Output: 2
Explanation:
After 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:
Input: nums = [1,3,5]
Output: 0
Explanation: nums is already sorted therefore, the answer is 0.
Example 3:
Input: nums = [2,1,4]
Output: -1
Explanation: It’s impossible to sort the array using right shifts.
Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100
nums contains distinct integers.
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
2855. Minimum Right Shifts to Sort the Array LeetCode Solution in C++
class Solution {
public:
int minimumRightShifts(vector<int>& nums) {
int count = 0;
int pivot = -1;
for (int i = 0; i + 1 < nums.size(); i++)
if (nums[i] > nums[i + 1]) {
++count;
pivot = i;
}
if (count == 0)
return 0;
if (count > 1 || nums.back() > nums.front())
return -1;
return nums.size() - 1 - pivot;
}
};
/* code provided by PROGIEZ */
2855. Minimum Right Shifts to Sort the Array LeetCode Solution in Java
class Solution {
public int minimumRightShifts(List<Integer> nums) {
int pivot = -1;
int count = 0;
for (int i = 0; i + 1 < nums.size(); i++)
if (nums.get(i) > nums.get(i + 1)) {
++count;
pivot = i;
}
if (count == 0)
return 0;
if (count > 1 || nums.get(nums.size() - 1) > nums.get(0))
return -1;
return nums.size() - 1 - pivot;
}
}
// code provided by PROGIEZ
2855. Minimum Right Shifts to Sort the Array LeetCode Solution in Python
class Solution:
def minimumRightShifts(self, nums: list[int]) -> int:
count = 0
for i, (a, b) in enumerate(itertools.pairwise(nums)):
if a > b:
count += 1
pivot = i
if count == 0:
return 0
if count > 1 or nums[-1] > nums[0]:
return -1
return len(nums) - pivot - 1
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.