[C++] ✅Easy O(n) Solution w/ Intuition + Code [Finding index]
Intuition\nMy intuition for this problem was to know the number of swaps to change the position of a number. Our task is to move the smallest number to the star
# Intuition\nMy intuition for this problem was to know the number of swaps to change the position of a number. Our task is to move the smallest number to the start and the largest number to the end of the array.\nTo move a number from index `i=5` to `j=0`, we need `i-j` swaps. So to move the minimum number from any index to the `0th index` we need as many number of swaps as the index of the min. number.\nNow we need `n-1-i` swaps to move the largest number(positioned at $$i^{th}$$ index) to the `n-1 index` but there\'s a catch if we do this after moving the min number.\n\nWe need to consider the fact that the largest number can come before the smallest number and that moving the smallest number has moved the largest number towards it\'s desired location. So if the largest number\'s index comes before the minimum number\'s index, we use `n-1-i - 1` to indicate that it has to do one less swap itself. \n\n# Approach\nThe approach is simple, we just find:\n- index of minimum number `minIdx`\n- index of maximum number `maxIdx`\n\nAnd use what we derived above:\n- Swaps for moving minimum number, `smin`: `minIdx`\n- Swaps for moving maximum number, `smax`: \n1. `n-1-maxIdx` if `minIdx<maxIdx`\n2. `n-1-maxIdx - 1` if `minIdx>maxIdx`\n\nSo,  Total number of swaps (our answer): `smin + smax`\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int semiOrderedPermutation(vector<int>& nums) {\n        int minIdx{0};\n        int maxIdx{0};\n        for(int i=0; i<nums.size(); i++) {\n            if (nums[i] < nums[minIdx])\n                minIdx = i;\n            if (nums[i] > nums[maxIdx])\n                maxIdx = i;\n        }\n        int res{0};\n        res += minIdx;\n        res += nums.size()-1-maxIdx-(maxIdx<minIdx);\n        return res;\n    }\n};\n```\n\n# Complexity\n- Time complexity: $$O(n)$$\nBecause we\'re traversing the array once to find the indices of the minimum and the maximum number.\n- Space complexity: $$O(1)$$\n\n\n> ### Note\n> - Upvote if you liked my solution\n> - Drop a comment if you have any doubt
['C++']