-
Notifications
You must be signed in to change notification settings - Fork 5
/
GFG_ARR_LeadersInArray.cpp
68 lines (51 loc) · 1.54 KB
/
GFG_ARR_LeadersInArray.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
57
58
59
60
61
62
63
64
65
66
67
68
/*
https://practice.geeksforgeeks.org/problems/leaders-in-an-array-1587115620/1
*/
// C++ program to remove recurring digits from
// a given number
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution{
//Function to find the leaders in the array.
public:
vector<int> leaders(int a[], int n){
int myf = a[n-1]; // max_yet_from_right
vector<int> ld; // saving leaders from right
ld.push_back(myf); //rightmost element is always leader
for(int i = n-2; i>=0; i--){
if(a[i] >= myf ) // greater than or equal to all the elements to its right side
{
myf=a[i];
ld.push_back(myf);
}
}
reverse(ld.begin(), ld.end()); // reversing saved leaders to match output
return ld;
}
};
// { Driver Code Starts.
int main()
{
long long t;
cin >> t;//testcases
while (t--)
{
long long n;
cin >> n;//total size of array
int a[n];
//inserting elements in the array
for(long long i =0;i<n;i++){
cin >> a[i];
}
Solution obj;
//calling leaders() function
vector<int> v = obj.leaders(a, n);
//printing elements of the vector
for(auto it = v.begin();it!=v.end();it++){
cout << *it << " ";
}
cout << endl;
}
}
// } Driver Code Ends