Easiest implementation
Complexity\n- Time complexity : O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity : O(1)\n Add your space complexity here, e.g. O(n) \n\n# 
# Complexity\n- Time complexity : $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity : $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        int n = nums.size();\n        int idx1,idxN;\n        for(int i=0 ; i<n ; i++) {\n            if(nums[i] == 1) idx1 = i;\n            if(nums[i] == n) idxN = i;\n        }\n        \n        int ans = idx1 + n-1-idxN;\n        if(idxN < idx1) ans--; // one common swap\n        return ans;\n    }\n};\n```
['C++']