Easy C++ Solution - Observation based
\n\n# Code\n\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        \n        int n = nums.size();\n        if(nums[0] == 1 an
\n\n# Code\n```\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        \n        int n = nums.size();\n        if(nums[0] == 1 and nums[nums.size() - 1] == nums.size()) {\n            return 0;\n        }\n        \n        int pos1 = -1;\n        int pos2 = -1;\n        \n        for(int i = 0; i < nums.size(); i++) {\n            if(nums[i] == 1) {\n                pos1 = i;\n            }\n            if(nums[i] == n) {\n                pos2 = i;\n            }\n        }\n        int ans = 0;\n        if(pos1 < pos2) {\n           ans = pos1 + (n - 1 - pos2); \n        }\n        \n        if(pos1 > pos2) {\n            int temp = pos1 - pos2;\n            pos1 = pos2;\n            pos2 = pos1 + 1;\n            \n            ans = pos1 + (n-1-pos2) + temp;\n        }\n        \n        return ans;\n        \n    }\n};\n```
['C++']