-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLeaders.java
More file actions
32 lines (26 loc) · 710 Bytes
/
Copy pathLeaders.java
File metadata and controls
32 lines (26 loc) · 710 Bytes
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Leaders {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int max=arr[n-1];
ArrayList<Integer> ans=new ArrayList<>();
ans.add(max);
for(int i=n-2;i>=0;i--)
{
if(arr[i]>max)
{
max=arr[i];
ans.add(max);
}
}
Collections.sort(ans,Collections.reverseOrder());
System.out.println(ans);
}
}