//{ Driver Code Starts
// 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)
{
vector<int>ans;
for(int i=0 ; i<n ; i++)
{
int j;
for( j=i+1 ; j<n ; j++)
{
if(a[i]<a[j])
{
break;
}
}
if(j==n)
{
ans.push_back(a[i]);
}
}
return ans;
}
};
//{ 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
//{ Driver Code Starts
// 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)
{
vector<int>v;
int maxi=INT_MIN;
for(int i=n-1 ; i>=0 ; i--)
{
if(a[i]>=maxi)
{
maxi=a[i];
v.push_back(maxi);
}
}
reverse(v.begin(),v.end());
return v;
}
};
//{ 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
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter