C++ solution
Intuition\n Describe your first thoughts on how to solve this problem. \nFor the given 0-indexed array, we are allowed to perform swap on any two adjacent eleme
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFor the given 0-indexed array, we are allowed to perform swap on any two adjacent elements unit we make array nums a semi-ordered permutation.\nwe can perform swap on element valued 1 till it reaches index 0.\nSimilarly, we perform swap on element valued n (which is also the size of the array nums) till it reaches the last index that is (n-1).\nWe return the number of swaps required in this process.\n\n\n<!-- Describe your approach to solving the problem. -->\n\n\n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\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 cnt=0;\n        bool f=false;\n        while(f==false){\n               for(int i=0;i<n;i++){\n                   if(nums[i]==n & i!=(n-1)){\n                       swap(nums[i],nums[i+1]);\n                       cnt++;\n                   }\n                   if(nums[i]==1 && i!=0){\n                       swap(nums[i], nums[i-1]);\n                       cnt++;\n                   }\n               }\n               if(nums[0]==1 && nums[n-1]==n){\n                   f=true;\n               }\n\n        }\n        return cnt;\n    }\n};\n```
['C++']