2717. Semi-Ordered Permutation
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```javascript []
/**
 * @param {number[]} nums
 * @return {number}
 */
var semiOrderedPermutation = function(nums) {
    // Find the index of 1 and n
    const n = nums.length;
    const indexOf1 = nums.indexOf(1);
    const indexOfN = nums.indexOf(n);

    // Number of swaps to move 1 to the front
    let swaps = indexOf1;

    // Number of swaps to move n to the end
    // If n is before 1 in the list, we need to subtract one swap to avoid double counting
    if (indexOfN > indexOf1) {
        swaps += (n - indexOfN - 1);
    } else {
        swaps += (n - indexOfN - 1) - 1;
    }

    return swaps;
};

```
['JavaScript']