Track min/max value ids
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```csharp []
public class Solution {
    public int SemiOrderedPermutation(int[] nums) {
        int len = nums.Length;
        int minIdx = -1, maxIdx = -1;
        for(int i = 0; i < len; i++)
        {
            if(minIdx < 0 || minIdx >= 0 && nums[minIdx] > nums[i])
                minIdx = i;

            if(maxIdx < 0 || maxIdx >= 0 && nums[maxIdx] < nums[i])
                maxIdx = i;
        }

        return minIdx + (len-1-maxIdx) - (minIdx > maxIdx ? 1 : 0);
    }
}
```
['C#']