TechoSagar: Leaders in an array

Search

Google Alert - jobs

Leaders in an array

Write a program to print all the LEADERS in the array. An element is leader if it is greater than all the elements to its right side. The rightmost element is always a leader. 
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print all the leaders.

Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= A[i]<=100
Example:
Input:
2
6
16 17 4 3 5 2
5
1 2 3 4 0
Output:
17 5 2
4 0
Your Code
#include<iostream>

using namespace std;

int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[100],i;
for(i=0; i<n; i++)
{
cin>>a[i];
}
int j;
for(i=0; i<n-1; i++)
{    int temp=1;
for(j=i+1; j<n; j++ )
{
if(a[i]<=a[j])
temp=0;
}
}
if(temp==1)
{
cout<<a[i]<<" ";
}
}
cout<<a[n-1]<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook