Problem
Minimum Operations to Sort a Permutation
Learn this problemProblem statement
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.
Function
minOperationsToSortPermutation(arr: int[]) → intExamples
Example 1
arr = [3,1,2]return = 1Move the first element 3 to the end to get [1,2,3].
Example 2
arr = [1,5,4,3,2]return = 2Move 1 to the end to get [5,4,3,2,1], then reverse the array to get [1,2,3,4,5].
Example 3
arr = [1,2,3,4]return = 0The 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
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Sort Bug Report FrequenciesOA · Seen Jul 2026
- Drone Delivery RouteOA · Seen Jul 2026