Easy JAVA 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:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        int n = nums.length, res = 0, i = 0;\n        while(nums[0] != 1) {\n            if(nums[i] == 1) {\n                res++;\n                nums[i] = nums[i-1];\n                nums[i-1] = 1; \n                i--;\n                continue;\n            }\n            i++;\n        }\n        i = 0;\n        while(nums[n-1] != n) {\n            if(nums[i] == n) {\n                res++;\n                nums[i] = nums[i+1];\n                nums[i+1] = n; \n            }\n            i++;\n        }\n        return res;\n    }\n}\n```
['Java']