C++ Solution
\n# Code\ncpp []\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        int idxOfOne, idxOfN, n = nums.size();\n        for(in
\n# Code\n```cpp []\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        int idxOfOne, idxOfN, n = nums.size();\n        for(int i = 0; i < n; i++){\n            if(nums[i] == 1) idxOfOne = i;\n            else if(nums[i] == n) idxOfN = i;\n        }\n        int res = idxOfOne + (n - 1 - idxOfN);\n        if(idxOfOne > idxOfN) res--;\n        return res;\n    }\n};\n```
['C++']