✔️ Python3 Solution | Find Position
Intuition\nFind the position of 1 and n in the nums let say l and r resp., no. of swaps required to make l the first-number is l since we need to swap all the e
# Intuition\nFind the position of 1 and n in the nums let say l and r resp., no. of swaps required to make l the first-number is `l` since we need to swap all the elements with 1 from index l to 0, Similarly for n be the last-number we required `n - r - 1` swaps.\nBut there\'s a catch if the index of n is less than 1 than we need to `subtract 1` from ans, as 1 swap is counted twice. When 1 and n are adjacent to each other after some operation\n\n# Complexity\n- Time complexity: $$O(n)$$\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution:\n    def semiOrderedPermutation(self, nums: List[int]) -> int:\n        n = len(nums)\n        l, r = nums.index(1), nums.index(n)\n        return l + n - r - 1 - (1 if r < l else 0)\n```
['Python', 'Python3']