Simple solution in Java. Beats 100 %
Complexity

Time complexity:
O(n)


Space complexity:
O(1)

Code
# Complexity
- Time complexity:
O(n)

- Space complexity:
O(1)

# Code
```java []
class Solution {
    public int semiOrderedPermutation(int[] nums) {
        int n = nums.length;
        int min = nums[0];
        int max = nums[0];
        for (int num : nums) {
            min = Math.min(min, num);
            max = Math.max(max, num);
        }
        int minIndex = 0;
        int maxIndex = 0;
        for (int i = 0; i < n; i++) {
            if (nums[i] == min) {
                minIndex = i;
            }
            if (nums[i] == max) {
                maxIndex = i;
            }
        }
        return minIndex < maxIndex ? (minIndex + (n - maxIndex - 1)) : (minIndex + (n - maxIndex - 1) - 1);
    }
}
```
['Array', 'Java']