C++  || Simple Solution
\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# 
\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        \n        int n = nums.size();\n        \n        int l = 0 , r = n - 1;\n\n        for(int i=0;i<n;i++){\n\n            if(nums[i] == 1) l = i;\n\n            if(nums[i] == n) r = i; \n        }\n\n        if(r < l){\n            return l + (n-1-r) - 1;\n        }\n\n        return l + (n-1-r);\n        \n    }\n};\n```
['Array', 'Math', 'C++']