Java | Easy solution | 5 lines
Complexity\n- Time complexity: O(n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity: O(1)\n Add your space complexity here, e.g. O(n) \n\n# Co
# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n  public int semiOrderedPermutation(int[] nums) {\n    int n = nums.length, a = 0, b = 0;\n    \n    for (var i=0; i<n; i++) {\n      if (nums[i] == 1) a = i;\n      if (nums[i] == n) b = i;\n    }\n    return a + (n-1-b) - (a > b ? 1 : 0);\n  }\n}\n```
['Array', 'Java']