Leaders in an array

QUESTION

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. \n\nInput:\nThe first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.\nThe first line of each test case contains a single integer N denoting the size of array.\nThe second line contains N space-separated integers A1, A2, …, AN denoting the elements of the array.\n\nOutput:\nPrint all the leaders.\n\nConstraints:\n1 <= T <= 100\n1 <= N <= 100\n0 <= A[i]<=100.

“TESTCASE_1”: “2\n6\n16 17 4 3 5 2\n5\n1 2 3 4 0\n###—###SEPERATOR—###—\n17 5 2\n4 0”, “TESTCASE_2”: “2\n6\n16 17 14 13 15 12\n5\n1 2 3 4 5\n###—###SEPERATOR—###—\n17 15 12\n5”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<iostream>
using namespace std;
 
int main(){
int t;
  cin>>t;
  int i;
  for(i=0;i<t;i++){
  int n,j;
    cin>>n;
    int a[n];
    for(j=0;j<n;j++){
    cin>>a[j];
    }
    int k;
    for(j=0;j<n-1;j++){
      int flag=0;
    for(k=j;k<n;k++){
    if(a[j]<a[k]){
    flag=1;
      break;
    }
    }
      if(flag==0){
      cout<<a[j]<<" ";
      }
     
    }
     cout<<a[n-1];
    cout<<"\n";
  }
  
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.