Sort a Permutation with Reversals and Left Rotations

Quick Overview

Find the fewest whole-array reversals and one-position left rotations needed to sort a permutation, returning minus one when its sorted order is unreachable.

Sort a Permutation with Reversals and Left Rotations

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Sort a Permutation with Reversals and Left Rotations You are given an array `nums` of length `n` containing every integer from `0` through `n - 1` exactly once. You may perform only these operations, each costing one operation: - Reverse the entire array. - Rotate the entire array left by one position: move its first element to the end and shift every other element one position left. Return the minimum number of operations needed to obtain `[0, 1, ..., n - 1]`. Return `-1` if this order cannot be reached. ### Function `int minOperations(vector<int>& nums)` ### Example 1 **Input:** `nums = [1, 2, 3, 4, 0]` **Output:** `3` **Explanation:** One optimal sequence is: 1. Reverse: `[0, 4, 3, 2, 1]`. 2. Rotate left once: `[4, 3, 2, 1, 0]`. 3. Reverse: `[0, 1, 2, 3, 4]`. Neither a single operation nor any two-operation sequence sorts the input. ### Example 2 **Input:** `nums = [0, 2, 4, 1, 3]` **Output:** `-1` **Explanation:** These operations preserve which values are neighbors when the array is viewed as a circle. Here the circular neighbors of `0` are `2` and `3`, while in the sorted array they would be `1` and `4`. The required order is therefore unreachable. ### Constraints - `1 <= n == nums.length <= 100000` - `0 <= nums[i] < n` - `nums` is a permutation of `0` through `n - 1`. ### Clarifications - A rotation by several positions requires that many one-position operations unless another sequence is cheaper. - Reversing a proper subarray and swapping individual elements are not allowed. - Reversals and rotations may be interleaved in any order, and an already sorted array needs `0` operations. ### What a Strong Answer Covers - Distinguishing unreachable inputs from inputs that merely need several operations. - Minimizing the number of allowed unit operations, including sequences with more than one reversal. - Supporting the full permutation length without materializing every intermediate array.

Overview: Find the fewest whole-array reversals and one-position left rotations needed to sort a permutation, returning minus one when its sorted order is unreachable.

Amazon logo
Amazon
Source window: 0–6 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Sort a Permutation with Reversals and Left Rotations

You are given an array nums of length n containing every integer from 0 through n - 1 exactly once.

You may perform only these operations, each costing one operation:

  • Reverse the entire array.
  • Rotate the entire array left by one position: move its first element to the end and shift every other element one position left.

Return the minimum number of operations needed to obtain [0, 1, ..., n - 1]. Return -1 if this order cannot be reached.

Function

int minOperations(vector<int>& nums)

Example 1

Input: nums = [1, 2, 3, 4, 0]

Output: 3

Explanation: One optimal sequence is:

  1. Reverse: [0, 4, 3, 2, 1] .
  2. Rotate left once: [4, 3, 2, 1, 0] .
  3. Reverse: [0, 1, 2, 3, 4] .

Neither a single operation nor any two-operation sequence sorts the input.

Example 2

Input: nums = [0, 2, 4, 1, 3]

Output: -1

Explanation: These operations preserve which values are neighbors when the array is viewed as a circle. Here the circular neighbors of 0 are 2 and 3, while in the sorted array they would be 1 and 4. The required order is therefore unreachable.

Constraints

  • 1 <= n == nums.length <= 100000
  • 0 <= nums[i] < n
  • nums is a permutation of 0 through n - 1 .

Clarifications

  • A rotation by several positions requires that many one-position operations unless another sequence is cheaper.
  • Reversing a proper subarray and swapping individual elements are not allowed.
  • Reversals and rotations may be interleaved in any order, and an already sorted array needs 0 operations.

What a Strong Answer Covers Guidance

  • Distinguishing unreachable inputs from inputs that merely need several operations.
  • Minimizing the number of allowed unit operations, including sequences with more than one reversal.
  • Supporting the full permutation length without materializing every intermediate array.