Easy Solution in Java
Code


# Code
```java []
class Solution {
    public int semiOrderedPermutation(int[] nums) {
        if(nums[0] == 1 && nums[nums.length - 1] == nums.length) return 0;
        int posOne = -1, posN = -1;
        for(int i=0; i<nums.length; i++){
            if(nums[i] == 1)
                posOne = i;
            else if(nums[i] == nums.length)
                posN = i;

            if((posOne != -1) && (posN != -1))
                break;
        }
        int swap = 0;
        if(posOne > posN)
            swap--;
        swap += posOne + (nums.length - 1 - posN);
        return swap;   
    }
}
```
['Java']