-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestEleK.java
More file actions
33 lines (31 loc) · 906 Bytes
/
LargestEleK.java
File metadata and controls
33 lines (31 loc) · 906 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
33
import java.util.*;
public class LargestEleK {
public static int maxEle(Deque<Integer> dq){
int max = Integer.MIN_VALUE;
for(int num : dq){
if(num > max){
max = num;
}
}
return max;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int []arr = new int[n];
ArrayList<Integer>ls = new ArrayList<>();
int k = sc.nextInt();
for(int i = 0; i < arr.length; i++) arr[i] = sc.nextInt();
Deque<Integer>dq = new ArrayDeque<>();
for(int right = 0; right < n; right++){
dq.add(arr[right]);
while(dq.size() > k){
dq.removeFirst();
}
if(dq.size() == k){
ls.add(maxEle(dq));
}
}
System.out.println(ls);
}
}