JavaScript 2 pointer
\nconst semiOrderedPermutation = function(nums) {\n  let i = 0, j = nums.length - 1\n  while(nums[i] != 1) i++\n  while(nums[j] != nums.length) j--\n  if(i < j)
```\nconst semiOrderedPermutation = function(nums) {\n  let i = 0, j = nums.length - 1\n  while(nums[i] != 1) i++\n  while(nums[j] != nums.length) j--\n  if(i < j) return nums.length - 1 - j + i\n  return nums.length - 2 - j + i\n};\n```
['JavaScript']