Java ⭐️ || Beginner Friendly Solution ✅ || 100% beats ✈️ || Fully Explained 💯
Approach\n\nTo convert the given permutation nums into a semi-ordered permutation, we need to perform swaps to move the number 1 to the first position and the n
# Approach\n\nTo convert the given permutation `nums` into a semi-ordered permutation, we need to perform swaps to move the number `1` to the first position and the number `n` to the last position. We can achieve this by finding the indices of `1` and `n` in the array and then calculating the number of operations required to move `1` to the first position and `n` to the last position.\n\n**Logic:**\n1. Initialize two variables `count` and `idx` to keep track of the number of operations and the index of `1` in the array, respectively.\n2. Loop through the array to find the index of the number `1`. Set `idx` to this index.\n3. Calculate the number of operations required to move `1` to the first position. Set `count` to `idx`.\n4. While the number at the first position is not `1`, swap the elements at indices `idx` and `idx-1` to move `1` towards the first position. Decrement `idx` by 1 after each swap.\n5. Loop through the array again to find the index of the number `n`. Set `idx` to this index.\n6. Calculate the number of operations required to move `n` to the last position. Return `count + n - idx - 1`, which represents the total number of operations needed to make `nums` a semi-ordered permutation.\n\n**Pseudocode:**\n```plaintext\nFunction semiOrderedPermutation(nums):\n    n = length of nums\n    count = 0\n    idx = 0\n    \n    # Find the index of 1 in nums\n    for i = 0 to n-1:\n        if nums[i] == 1:\n            idx = i\n            break\n    \n    # Calculate the number of operations to move 1 to the first position\n    count = idx\n    \n    # Move 1 to the first position using swaps\n    while nums[0] != 1:\n        swap nums[idx] and nums[idx-1]\n        idx = idx - 1\n    \n    # Find the index of n in nums\n    for i = 0 to n-1:\n        if nums[i] == n:\n            idx = i\n            break\n    \n    # Calculate the number of operations to move n to the last position\n    return count + n - idx - 1\n```\n\n**Final Remarks:**\nThe provided solution follows the above approach and logic to solve the problem. It efficiently moves the number `1` to the first position and the number `n` to the last position, minimizing the number of swaps needed. The solution is clear and concise, making it easy for beginners to understand and implement.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        int n = nums.length;\n        int count = 0;\n        int idx = 0;\n        \n        for (int i = 0; i < n; i++) {\n            if (nums[i] == 1) {\n                idx = i;\n                break;\n            }\n        }\n        \n        count = idx;\n\n        while (nums[0] != 1) {\n            nums[idx] = nums[idx - 1];\n            nums[idx - 1] = 1;\n            idx--;\n        }\n        \n        for (int i = 0; i < n; i++) {\n            if (nums[i] == n) {\n                idx = i;\n                break;\n            }\n        }\n\n        return count + n - idx - 1;\n    }\n}\n```
['Array', 'Simulation', 'Java', 'C#']