Simple JS Code!!!
IntuitionLets find out the index of 1 and n then we can follow the linear equations to find the number of total swaps needed.ApproachlengthOfNums: Stores the le
# Intuition
Lets find out the index of 1 and n then we can follow the linear equations to find the number of total swaps needed.

# Approach
lengthOfNums: Stores the length of the nums array.

x: Will store the index of the number 1 in the nums array.

y: Will store the index of the largest number in the array, which is lengthOfNums (the number equal to the array length).

swap: Will store the calculated result, which is the final value returned by the function.

The loop iterates through each index i of the nums array.

If the value at nums[i] is 1, it assigns the index i to x.

If the value at nums[i] is equal to lengthOfNums, it assigns the index i to y.

Conditional (ternary) operator that calculates the value of swap:

The expression calculates the number of "swaps" (or steps) needed to move 1 and the largest number (lengthOfNums) to their correct positions in an almost sorted sequence.

The formula used for swap:

x: the index of 1 in the array.

y: the index of the largest number (lengthOfNums).

lengthOfNums - y - 1: the number of elements from the largest number to the end of the array.

x + (lengthOfNums - y - 1): the total number of steps to move both 1 and lengthOfNums to their correct positions.

The subtraction by 1 (- 1) if x > y is needed to avoid over-counting when x is ahead of y.

Finally, the swap value is returned, which represents the number of "swaps" or steps needed to semi-order the permutation.

# Complexity
- Time complexity:
O(N)

- Space complexity:
O(1)

# Code
```javascript []
/**
 * @param {number[]} nums
 * @return {number}
 */
var semiOrderedPermutation = function(nums) {
    const lengthOfNums = nums.length
    let x
    let y
    let swap
    for(let i = 0; i<lengthOfNums; i++){
        if(nums[i] === 1){
            x = i
        }
        if(nums[i] === lengthOfNums){
            y = i
        }
    }
    x > y ? swap = x + (lengthOfNums-y-1) - 1 : swap = x + (lengthOfNums-y-1)
    return swap
};
```
['JavaScript']