1 ms solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```java []\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n\n        int n = nums.length, f = 0, l = 0;\n        for (int i = 0; i < n; i++)\n            if (nums[i] == 1)\n                f = i;\n            else if (nums[i] == n)\n                l = i;\n            \n        int k = f < l ? 1 : 2;\n        \n        return f + n - l - k;\n    }\n}\n```
['Java']