Explained - iteration || Very simple and easy to understand
Approach\n1. Find the ith index of min val 1 => we need to do i operation to move 1 to first position\n2. Find the ith index of the max val n => we need to do (
## Approach\n1. Find the ith index of min val 1 => we need to do i operation to move 1 to first position\n2. Find the ith index of the max val n => we need to do (n - i - 1) operation to take it to last position\n3. Take the sum of above two as the answer. Only corner case is when the ith index of 1 is larger than ith index of n - in this case we need to reduce the ans by 1 as the 1 & n cna be swapped at the same time so that we count it as 1 operation in place of 2.\n \n# Code\n```\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        int imin = 0, imax = nums.size(), n = nums.size();\n        for(int i = 0; i < n; ++i){\n            if(nums[i] == 1) imin = i;\n            if(nums[i] == n) imax = i;\n        }\n        if(imin < imax) return (imin + n - imax - 1);\n        return (imin + n - imax - 1) - 1;\n    }\n};\n```
['C++']