Strongest Fighter Problem

import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt(), arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = cin.nextInt();
}
int k = cin.nextInt();
LinkedList li = new LinkedList<>();
int i = 0;
int max = Integer.MIN_VALUE;
for(;i<k;i++){
if(arr[i]>max){
max = arr[i];
li.addLast(i);
}
}
for(;i<arr.length;i++){
if(!li.isEmpty()){
System.out.print(arr[li.peek()]);
}
while(!li.isEmpty()&&li.peek()<=i-k){
li.removeFirst();
}
if(arr[i]>max){
max = arr[i];
li.addLast(i);
}
}
}
}---------What’s the error in the code

@AbhishekAhlawat1102,
I have corrected your code: https://ide.codingblocks.com/s/194389
Errors:

  1. No need to use max variable. Since you need to find the max in a window.

  2. For every element added, the previous smaller elements are not useful so we remove them from out linked list li. You were only traversing the first k elements in your for loop, instead you need to take k elements in a window. For example 5 elements exist: In the first window you take indexes (0,1,2), in the second window indexes (1,2,3), in the third window indexes (2,3,4). And then we have to print the maximum of each window.

  3. We remove all elements smaller than the current element being added (remove useless elements).