Simple java code 1 ms beats 100 %
\n# Complexity\n- \n\n# Code\n\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        int x=-1,y=-1;\n        for(int i=0;i<nums.length
\n# Complexity\n- \n![image.png](https://assets.leetcode.com/users/images/233e2ae4-ae8d-426a-a792-2ad3e10cc547_1705985140.33696.png)\n# Code\n```\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        int x=-1,y=-1;\n        for(int i=0;i<nums.length;i++){\n            if(nums[i]==1) x=i;\n            else if(nums[i]==nums.length) y=i;\n        }\n        if(x<y) return x+(nums.length-1-y);\n        return x+(nums.length-1-y)-1;\n    }\n}\n```
['Java']