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.
COMPANY:PayU
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.
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.
Print all the leaders.
Constraints:
1 <= T <= 100
1 <= N <= 100
0 <= A[i]<=100
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
Input:
2
6
16 17 4 3 5 2
5
1 2 3 4 0
Output:
17 5 2
4 0
CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt();
for(int mi=0;mi<m;mi++){
int n=sc.nextInt();
int [] ar= new int [n];
int [] count=new int[n];
Arrays.fill(count,1);
for(int i=0;i<n;i++){
ar[i]=sc.nextInt();
}
for(int j=0;j<n;j++){
for(int k=j+1;k<n;k++){
if(ar[j]>ar[k]){count[j]+=1;};
}
}
for(int j1=0;j1<n;j1++){
if(count[j1]==n-j1){System.out.print(ar[j1]+" ");};
}
System.out.println();
}
}
}
EXECUTION TIME:0.16s
Comments
Post a Comment