Super Easy Solution with Simple Logic || Store positions
Intuition\nSince we have to swap adjacent numbers to fulfil the given condition ,we will store the psoiton of 1 and the last number by iterating through loop. A
# Intuition\nSince we have to swap adjacent numbers to fulfil the given condition ,we will store the psoiton of 1 and the last number by iterating through loop. After the answer will be total number of swaps,hence we will add the number of swaps with the help of their positions.\nBut if the last number i.e. n is present before the number 1 both of the them reach their destinantion simultaneously ,hence we will decrease the answer by 1.\n\n# Approach\n- We will store the two psoitons in variables f and l.\n- We will store the answer i.e. total number of swaps in variable\n```\nint ans=f+(n-1-l);\n```\n- We will check for the condition (mentioned in Intuiton above) and decrement answer by 1 if it is there.\n```\nif(l<f)\n{\n    ans=ans-1;\n}\n```\n- Return the answer.\n\n\n\n\n\n\n# Complexity\n- Time complexity:It will be O(n) because of for loop.\n\n\n- Space complexity:It wil be O(1) .\n\n# Code\n```\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        int n=nums.size();\n        int f=0,l=0;\n        for(int i=0;i<n;i++)\n        {\n            if(nums[i]==1)\n            {\n                f=i;\n            }\n            if(nums[i]==n)\n            {\n                l=i;\n            }\n        }\n        int ans=f+(n-1-l);\n        if(l<f)\n        {\n            ans=ans-1;\n        }\n        return ans;\n    }\n};\n```\nPlease upvote if it helps!
['Array', 'C++']