-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaders_in_array.cpp
56 lines (46 loc) · 984 Bytes
/
leaders_in_array.cpp
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;
class Solution{
public:
vector<int> leaders(int a[], int n){
vector<int> vec;
int mx = a[n-1];
vec.push_back(mx);
for(int i=n-2; i>=0; i--){
if(a[i]>=mx)
{
vec.push_back(a[i]);
}
mx = max(a[i],mx);
}
int i=0;
int j = vec.size()-1;
while(i<j)
{
swap(vec[i],vec[j]);
i++;
j--;
}
return vec;
}
};
int main()
{
long long t;
cin >> t;
while (t--)
{
long long n;
cin >> n;
int a[n];
for(long long i =0;i<n;i++){
cin >> a[i];
}
Solution obj;
vector<int> v = obj.leaders(a, n);
for(auto it = v.begin();it!=v.end();it++){
cout << *it << " ";
}
cout << endl;
}
}