-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray-01
More file actions
76 lines (50 loc) · 1.94 KB
/
Array-01
File metadata and controls
76 lines (50 loc) · 1.94 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*Geetha wants the know the best leaders in the given list.There is some condition to find the best leaders. Each leader has a score on their performance. Just take the score of the leader in reverse order and take the first score that compare with the other one who has the maximum score of the first one. Now take the current maximum score as the best leader score same as compared with the others.
Explanation: arr={16,17,4,7,3,5,2} First take the last element from the list 2,When taking the score of 2 that compare with other,5 is the maximum of 2 Now the current maximum of score is 5,When taking the score of 5 that compare with other,7 is maximum of 5. Now the current maximum of score is 7,when taking the score of 7 that compare with other,17 is maximum of 7.
Then finally the best Leader scores are {2,5,7,17}
Input Format
First Input corresponds as no of leaders. Second input corresponds as score of the each leader.
Constraints
No Constraints
Output Format
Find the best leaders score.
Sample Input 0
7
16 17 7 4 5 3 2
Sample Output 0
The Best Leaders are 2 3 5 7 17
Sample Input 1
7
1 9 5 3 6 2 4
Sample Output 1
The Best Leaders are 4 6 9
Sample Input 2
6
6 3 1 4 7 8
Sample Output 2
The Best Leaders are 8*/
#Answer
import java.io.*;
import java.util.*;
public class Solution {
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();
}
ArrayList<Integer> leaders = new ArrayList<>();
int max = Integer.MIN_VALUE;
for(int i = n-1; i >= 0; i--){
if(arr[i] >= max){
leaders.add(arr[i]);
max = arr[i];
}
}
Collections.sort(leaders);
System.out.print("The Best Leaders are ");
for(int x : leaders){
System.out.print(x + " ");
}
}
}