Positions solution for C# (explanation+complexity)
Approach\nThe idea is to find the positions of the element with a value 1 and the element with a value n.\n The first position is the number of iterations neede
# Approach\nThe idea is to find the positions of the element with a value 1 and the element with a value n.\n* The first position is the number of iterations needed to move an element from its current position to the zero position.\n* The (nums.length - second position) is the number of iterations needed to move an element from its current position to the last position.\n\nThe corner case is when the first position is larger than the second one. So, we use one swap to change both elements at once.\n\n# Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n\n# Code\n```\npublic int SemiOrderedPermutation(int[] nums)\n{\n    var leftIndex = -1;\n    var rightIndex = -1;\n\n    // Find positions\n    for (var i = 0; i < nums.Length; i++)\n    {\n        if (nums[i] == 1)\n        {\n            leftIndex = i;\n        }\n        else if (nums[i] == nums.Length)\n        {\n            rightIndex = i;\n        }\n        else if (leftIndex != -1 && rightIndex != -1)\n        {\n            break;\n        }\n    }\n\n    // Check if already semi-ordered\n    if (leftIndex == 0 && rightIndex == nums.Length - 1)\n    {\n        return 0;\n    }\n\n    // Calc number of iterations\n    return leftIndex + (nums.Length - rightIndex - 1) + (leftIndex > rightIndex ? -1 : 0);\n}\n```
['C#']