Java&JS&TS Solution (JW)
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```java []\npublic int semiOrderedPermutation(int[] nums) {\n    int n = nums.length;\n    int index1 = -1;\n    int indexN = -1;\n\n    // Find the positions of 1 and n in the array\n    for (int i = 0; i < n; i++) {\n        if (nums[i] == 1) index1 = i;\n        if (nums[i] == n) indexN = i;\n    }\n\n    // Calculate moves to bring 1 to the start and n to the end\n    int moves = index1 + (n - 1 - indexN);\n\n    // If `1` is before `n`, no extra move needed; otherwise, one move overlaps\n    if (index1 > indexN) moves -= 1;\n\n    return moves;\n}\n```\n```javascript []\nvar semiOrderedPermutation = function(nums) {\n    const n = nums.length;\n    let index1 = -1;\n    let indexN = -1;\n\n    // Find the positions of 1 and n in the array\n    for (let i = 0; i < n; i++) {\n        if (nums[i] === 1) index1 = i;\n        if (nums[i] === n) indexN = i;\n    }\n\n    // Calculate moves to bring 1 to the start and n to the end\n    let moves = index1 + (n - 1 - indexN);\n\n    // If `1` is before `n`, no extra move needed; otherwise, one move overlaps\n    if (index1 > indexN) moves -= 1;\n\n    return moves;\n};\n```\n```typescript []\nfunction semiOrderedPermutation(nums: number[]): number {\n    const n = nums.length;\n    let index1 = -1;\n    let indexN = -1;\n\n    // Find the positions of 1 and n in the array\n    for (let i = 0; i < n; i++) {\n        if (nums[i] === 1) index1 = i;\n        if (nums[i] === n) indexN = i;\n    }\n\n    // Calculate moves to bring 1 to the start and n to the end\n    let moves = index1 + (n - 1 - indexN);\n\n    // If `1` is after `n`, one move overlaps, so we subtract 1\n    if (index1 > indexN) moves -= 1;\n\n    return moves;\n};\n```
['Array', 'Java', 'TypeScript', 'JavaScript']