#include<bits/stdc++.h>
using namespace std;
vector<int> printLeaders(int arr[], int n) {
vector<int> ans;
// Last element of an array is always a leader,
// push into ans array.
int max = arr[n - 1];
ans.push_back(arr[n-1]);
// Start checking from the end whether a number is greater
// than max no. from right, hence leader.
for (int i = n - 2; i >= 0; i--)
if (arr[i] > max) {
ans.push_back(arr[i]);
max = arr[i];
}
return ans;
}
int main() {
// Array Initialization.
int n = 6;
int arr[n] = {10, 22, 12, 3, 0, 6};
vector<int> ans = printLeaders(arr,n);
for(int i = ans.size()-1;i>=0;i--){
cout<<ans[i]<<" ";
}
cout<<endl;
return 0;
}
CgoKI2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZlY3RvcjxpbnQ+IHByaW50TGVhZGVycyhpbnQgYXJyW10sIGludCBuKSB7CgogIHZlY3RvcjxpbnQ+IGFuczsKICAKIC8vIExhc3QgZWxlbWVudCBvZiBhbiBhcnJheSBpcyBhbHdheXMgYSBsZWFkZXIsCiAvLyBwdXNoIGludG8gYW5zIGFycmF5LgogaW50IG1heCA9IGFycltuIC0gMV07CiBhbnMucHVzaF9iYWNrKGFycltuLTFdKTsKCiAgLy8gU3RhcnQgY2hlY2tpbmcgZnJvbSB0aGUgZW5kIHdoZXRoZXIgYSBudW1iZXIgaXMgZ3JlYXRlcgogIC8vIHRoYW4gbWF4IG5vLiBmcm9tIHJpZ2h0LCBoZW5jZSBsZWFkZXIuCiAgZm9yIChpbnQgaSA9IG4gLSAyOyBpID49IDA7IGktLSkKICAgIGlmIChhcnJbaV0gPiBtYXgpIHsKICAgICAgYW5zLnB1c2hfYmFjayhhcnJbaV0pOwogICAgICBtYXggPSBhcnJbaV07CiAgICB9CgogIAogIHJldHVybiBhbnM7Cn0KCmludCBtYWluKCkgewogICAgCiAgLy8gQXJyYXkgSW5pdGlhbGl6YXRpb24uCiAgaW50IG4gPSA2OwogIGludCBhcnJbbl0gPSB7MTAsIDIyLCAxMiwgMywgMCwgNn07CgogIHZlY3RvcjxpbnQ+IGFucyA9IHByaW50TGVhZGVycyhhcnIsbik7CiAgCiAgCiAgZm9yKGludCBpID0gYW5zLnNpemUoKS0xO2k+PTA7aS0tKXsKICAgICAgCiAgICAgIGNvdXQ8PGFuc1tpXTw8IiAiOwogIH0KICAKICBjb3V0PDxlbmRsOwogIHJldHVybiAwOwp9Cgo=