[AlgoExpert] Subarray Sort

Subarray Sort

  • Time : O(n)
  • Space : O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <vector>
using namespace std;

vector<int> subarraySort(vector<int> array) {
int mi = INT_MAX, ma = INT_MIN, n = array.size();
int minpos = -1, maxpos = -1;

for(int i = 0; i < n; i++) {
if(array[i] < ma) {
maxpos = i;
}
ma = max(array[i], ma);
}
for(int i = n - 1; i >= 0; i--) {
if(array[i] > mi) {
minpos = i;
}
mi = min(array[i], mi);
}

return {minpos, maxpos};
}

Author: Song Hayoung
Link: https://songhayoung.github.io/2022/05/12/PS/AlgoExpert/subarray-sort/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.