Easy (with comments)
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
```typescript []
function semiOrderedPermutation(nums: number[]): number {
    const n = nums.length;

    // Find the indices of 1 and n
    const index1 = nums.indexOf(1);
    const indexN = nums.indexOf(n);

    // Calculate swaps needed for both
    if (index1 < indexN) {
        // If `1` is to the left of `n`, no overlap
        return index1 + (n - 1 - indexN);
    } else {
        // If `1` is to the right of `n`, overlap occurs
        return index1 + (n - 1 - indexN) - 1;
    }
}

```
['TypeScript']