JS | Swapping | Runtime 4 ms | Beats 100.00% | Bloated Code warning
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar semiOrderedPermutation = function (nums) {\n    if (!nums.includes(1) || !nums.includes(nums.length)) return false;\n    let oneIndex = nums.indexOf(1);\n    \n    let minSteps = 0;\n    while (nums[0] !== 1) {\n        let indexOfItemBeforeOne = oneIndex - 1;\n        let itemBeforeOne = nums[indexOfItemBeforeOne];\n        //console.log(\'>> swapping\', itemBeforeOne, \'at\', indexOfItemBeforeOne, \'with\', nums[oneIndex], \'at\', oneIndex)\n        nums[indexOfItemBeforeOne] = nums[oneIndex];\n        nums[oneIndex] = itemBeforeOne;\n        oneIndex = indexOfItemBeforeOne;\n        minSteps++;\n    }\n    let lastindex = nums.indexOf(nums.length);\n    while (nums[nums.length - 1] !== nums.length) {\n        let indexOfItemAfterMax = lastindex + 1;\n        let itemAfterMax = nums[indexOfItemAfterMax];\n        nums[indexOfItemAfterMax] = nums[lastindex];\n        nums[lastindex] = itemAfterMax;\n        lastindex = indexOfItemAfterMax;\n        minSteps++;\n    }\n    return minSteps;\n};\n```
['JavaScript']