You are given a permutation arr of size n, containing each integer from 1 to n exactly once.
In one operation, you may do either of the following:
- Move the first element of the array to the end, shifting every other element one position to the left.
- Reverse the entire array.
It is guaranteed that the array can be sorted into increasing order using these operations. Return the minimum number of operations needed to sort arr.
Examples
01 · Example 1
arr = [3,1,2] return = 1
Move the first element 3 to the end to get [1,2,3].
02 · Example 2
arr = [1,5,4,3,2] return = 2
Move 1 to the end to get [5,4,3,2,1], then reverse the array to get [1,2,3,4,5].
03 · Example 3
arr = [1,2,3,4] return = 0
The array is already sorted.
Constraints
1 <= arr.length <= 10^51 <= arr[i] <= arr.lengtharris a permutation of integers from1toarr.length.- It is guaranteed that
arrcan be sorted using the given operations.
More Amazon problems
- Count Promotional PeriodsOA · Seen Jun 2026
- Find Maximum Total Amount (SDE I, Fungible :)Seen Jun 2026
- Get Minimum AmountOA · Seen Jun 2026
- Find Minimum CostOA · Seen Jun 2026
- Get Smallest Base SegmentOA · Seen Jun 2026
- Select Least Resource TasksOA · Seen Jun 2026
- Product Category Group SizesPHONE SCREEN · Seen May 2026
- Count Connected ComponentsPHONE SCREEN · Seen May 2026
public int minOperationsToSortPermutation(int[] arr) {
// write your code here
}arr[3,1,2]
expected1
sign in to submit