File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ package SummerTrainingGFG .Arrays ;
2
+
3
+ import java .io .BufferedReader ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStreamReader ;
6
+ import java .util .ArrayList ;
7
+
8
+ /**
9
+ * @author Vishal Singh */
10
+ public class LeadersInArray {
11
+ static void leaderInArray (int [] arr ,int n ){
12
+ ArrayList <Integer > arrayList = new ArrayList <>(n );
13
+ int currLeader = arr [n -1 ];
14
+ arrayList .add (currLeader );
15
+ for (int i = n -2 ; i >= 0 ; i --) {
16
+ if (arr [i ]>=currLeader ){
17
+ currLeader = arr [i ];
18
+ arrayList .add (currLeader );
19
+ }
20
+ }
21
+ for (int i = arrayList .size ()-1 ; i >=0 ; i --) {
22
+ System .out .print (arrayList .get (i )+" " );
23
+ }
24
+ System .out .println ();
25
+ }
26
+ public static void main (String [] args )throws IOException {
27
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
28
+ int testCase = Integer .parseInt (br .readLine ());
29
+ while (testCase -->0 ){
30
+ int n = Integer .parseInt (br .readLine ());
31
+ String [] input = br .readLine ().split (" " );
32
+ int [] arr = new int [n ];
33
+ for (int i = 0 ; i < n ; i ++) {
34
+ arr [i ] = Integer .parseInt (input [i ]);
35
+ }
36
+ leaderInArray (arr ,n );
37
+ }
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments