STEP BY STEP EXPLANATION WITH EXAMPLE
Intuition\n Describe your first thoughts on how to solve this problem. \nWe just need to find the index of both the elements min element=1\nand maxelement=nums.
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**We just need to find the index of both the elements min element=1\nand maxelement=nums.size();**\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n# There are two cases only\n1. If location of max element  is right to location of minimum  element like **[2,3,1,7,6,5,4]** *here 7 is at right of 1*\nin this case answer is **n-maxIndex+minindex** i.e \n**Index of 7 is 3 index of 1 is 2 and n=6 (last index of array)**\n    6-3+2=5\n\n\n2. If location of max is left of location of minimum like **[2,3,7,4,5,1,6]**\nin this case answer is **n-maxIndex+minIndex-1** i.e \n**Index of 7 is 2 index of 1 is 5 and n=6 (last index of array)**\n 6-2+5-1=8\n\n### here 1 is subtracted because when we swap max element with elements to put it at right place it will be swapped once with min elelment\n**[2,3,7,4,5,1,6]**\nhere ist swap **[2,3,4,7,5,1,6]**\nhere 2nd swap **[2,3,4,5,7,1,6]**\nhere 3rd swap **[2,3,4,5,1,7,6]**\n**Now look here the minimum element is changing it\'s postion\nSo -1 is done to count this** \n\n\n\n\n**Here 7 is max element\n 1 is min element**\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$  Constant space\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n# C++\n```\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n               int min=-1,max=-1,ans;\n               int n=nums.size()-1;\n        for(int i=0; i<nums.size(); i++){\n            if(nums[i]==1)\n                        {\n                min=i;\n                        } //if\n            if(nums[i]==nums.size())\n                       {\n                        max=i;\n                            }//if\n                \n        if(min!=-1&&max!=-1){break;}//to break the loop \n        }//for\n        ans=min+(n-max);\n        if(max<min) \n             {\n             return ans-1;\n             }\n    \n     return ans; \n        \n    }\n};\n```\n# JAVA\n```\nclass Solution {\n    public int semiOrderedPermutation(int[] nums) {\n        int max = Arrays.stream(nums).max().getAsInt();\n        int indexMin=0;\n        int indexMax=0;\n        for(int i=0;i<nums.length;i++){\n            if(nums[i]==1){\n                indexMin=i;\n            }\n            if(nums[i]==max){\n                indexMax=i;\n            }\n        }\n        if(indexMax<indexMin){\n            return indexMin + nums.length-1 -indexMax-1;\n        }\n        else{\n            return indexMin + nums.length-1 -indexMax;\n        }\n        \n    }\n}\n```\n# UPVOTE IF IT\'S HELPFUL\n\n
['Array', 'Two Pointers', 'C++', 'Java']