Minimum Operations to Sort a Permutation
Updated
Problem 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 Online Assessment questions
- Maximize K-Element AND with an Increment BudgetOA · Seen Nov 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · Seen Nov 2026
- Get Smallest Base SegmentOA · Seen Nov 2026
- Minimum Cost of Left and Right PropagationOA · Seen Nov 2026
- Minimum Grid InconvenienceOA · Seen Nov 2026
- Maximize Shipment Order by Sorting One WindowOA · Seen Nov 2026
- Maximum Equal-Bit Pairs in a SubsequenceOA · Seen Nov 2026
- First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Nov 2026