NewThe Learning module is now liveSee what's new

Given an integer array arr of n distinct integers, you have exactly two operations available, each counted as one step:

  • Rotate: Move the first element to the end of the array. For example, [a, b, c, d] becomes [b, c, d, a].
  • Flip: Reverse the entire array. For example, [a, b, c, d] becomes [d, c, b, a].

You may apply these operations in any order and any number of times. Return the minimum number of steps to sort arr in non-decreasing order, or -1 if it is not possible to reach the sorted order using these operations.

Constraints:

  • 1 ≤ n ≤ 8
  • −104-10^4 ≤ arr[i] ≤ 10410^4

Example 1:

Input: arr = [3, 2, 1] Output: 1 Explanation: Applying Flip once transforms [3, 2, 1] into [1, 2, 3], which is sorted.

Example 2:

Input: arr = [2, 1, 4, 3] Output: 3

Example 3:

Input: arr = [2, 1, 3] Output: 2


Accepted
237/689
Acceptance Rate34.4%
Test Cases
Input
arr =
Output
Expected