Simple Java O(n) Solution
Approach\n- Get the index of 1 and N\n- Number of swaps would be sum of difference between index of 1 and 0 and difference between index of N and last index.\n-
# Approach\n- Get the index of 1 and N\n- Number of swaps would be sum of difference between index of 1 and 0 and difference between index of N and last index.\n- If position of N is before 1, then we require 1 less swap\n\n# Code\n```\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        int indexOne = -1;\n        int indexN = -1;\n        \n        for(int i=0; i<nums.length; i++) {\n            if(nums[i] == 1)\n                indexOne = i;\n            if(nums[i] == nums.length)\n                indexN = i;\n        }\n        \n        int count = 0;\n        if(indexOne > indexN)\n            count--;\n        \n        count += indexOne + (nums.length - 1 - indexN);\n        return count;\n    }\n}\n```
['Java']