Easy solution in java (O(n))
IntuitionTake care of 1st and last value where it is present if suppose of index of 1 is i and index of n is j.
and if i<j it means just we need to move 1 to 0
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Take care of 1st and last value where it is present if suppose of index of 1 is i and index of n is j.
and if i<j it means just we need to move 1 to 0 position and j to nth position.
else 
  (just we need to move 1 to 0 position and j to nth position)-1
  As 1 will be common step for both.
# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(1)
# Code
```java []
class Solution {
    public int semiOrderedPermutation(int[] nums) {
        int indexOfOne = -1;
        int indexOfNumsLength = nums.length;
        for(int i=0;i<nums.length;i++) {
            if(nums[i]==1) {
                indexOfOne = i;
            }
            if(nums[i]==nums.length) {
                indexOfNumsLength = i;
            }
        }

        if(indexOfOne<indexOfNumsLength) {
            return indexOfOne + (nums.length-indexOfNumsLength-1);
        }
        else {
            return indexOfOne + (nums.length-indexOfNumsLength-1) -1;
        }
    }
}
```
['Java']