-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeakArray.cpp
More file actions
56 lines (52 loc) · 1.11 KB
/
PeakArray.cpp
File metadata and controls
56 lines (52 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<bits/stdc++.h>
using namespace std;
vector<int> peakArray(vector<int> &a){
int n = a.size();
auto it = minmax_element(a.begin() ,a.end());
int max_idx = distance(a.begin() , it.second);
int mx = a[max_idx];
vector<int> temp;
vector<int> op;
for (int i = 0; i < max_idx; i++)
{
temp.push_back(a[i]);
}
sort(temp.begin(),temp.end());
for (int i = 0; i < temp.size(); i++)
{
op.push_back(temp[i]);
}
temp.clear();
op.push_back(mx);
for (int i = max_idx+1; i < n; i++)
{
temp.push_back(a[i]);
}
sort(temp.begin(), temp.end(), greater<int>());
for (int i = 0; i < temp.size(); i++)
{
op.push_back(temp[i]);
}
temp.clear();
return op;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
vector<int> arr(n);
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
vector<int> op = peakArray(arr);
// cout << candyDivision(arr) << endl;
for(auto it: op){
cout<<it<<" ";
}
}
}