Skip to content

Practice Minimum Right Shifts to Sort the Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2855
LeetCode Problem

Minimum Right Shifts to Sort the Array

Problem 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.

Source: Minimum Right Shifts to Sort the Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [3,4,5,1,2]
Output
2

# Example 2

Input
nums = [1,3,5]
Output
0

# Example 3

Input
nums = [2,1,4]
Output
-1

Constraints

1 <= nums.length <= 100
1 <= nums[i] <= 100
nums contains distinct integers.

Related LeetCode Problems

Practice more problems similar to Minimum Right Shifts to Sort the Array.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.