-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayLeader.java
51 lines (43 loc) · 1.35 KB
/
ArrayLeader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Stack;
/*
Name: Leaders In An Array
Source: GeeksForGeeks
Link: https://practice.geeksforgeeks.org/problems/leaders-in-an-array/0
Statement: An element of array is leader if it is greater than or equal to all
the elements to its right side. Also, the rightmost element is always a leader.
*/
public class ArrayLeader {
public static void main(String[] args) throws java.io.IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int test = Integer.parseInt(br.readLine());
Stack<Integer> stk = new Stack<>();
while(test>0)
{
int n = Integer.parseInt(br.readLine());
int [] arr = new int [n];
String [] st = (br.readLine()).split(" ");
for(int i =0; i<n; i++)
{
arr[i] = Integer.parseInt(st[i]);
}
int max = -1;
for(int i =n-1; i>=0; i--)
{
if(max<=arr[i])
{
max = arr[i];
stk.push(max);
}
}
while(!stk.empty())
{
System.out.print(stk.pop()+" ");
}
System.out.println("");
test--;
}
}
}