Skip to content

Practice Semi-Ordered Permutation

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2717
LeetCode Problem

Semi-Ordered Permutation

Problem Statement

Given a permutation of integers 1 through n, swap adjacent elements as often as needed to put 1 first and n last. Other positions may remain in any order. Return the fewest swaps required.

Source: Semi-Ordered Permutation on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,1,4,3]
Output
2

# Example 2

Input
nums = [2,4,1,3]
Output
3

# Example 3

Input
nums = [1,3,4,2,5]
Output
0

Constraints

2 <= nums.length == n <= 50
1 <= nums[i] <= 50
nums is a permutation.

Related LeetCode Problems

Practice more problems similar to Semi-Ordered Permutation.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.