TLE and wrong answers

"import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scn.nextInt();
}
int k = scn.nextInt();
slidingWindow(arr, k);
}

public static void slidingWindow(int arr[], int k) {
	LinkedList<Integer> q = new LinkedList<Integer>();
	int max = Integer.MIN_VALUE;
	for (int i = 0; i < k; i++) {
		max = Math.max(max, arr[i]);
	}
	q.addLast(max);
	for (int i = 1; i <= arr.length - k; i++) {
		System.out.print(q.getFirst() + " ");
		if (arr[i + k - 1] > q.getFirst()) {
			q.removeFirst();
			q.addLast(arr[i + k - 1]);
		}
	}
	if(!q.isEmpty()) {
		System.out.print(q.getFirst());
	}

}

}
"

Your logic is wrong. You can’t just decide if this particular element is the maximum of any subarray of size k by keeping just single element in the LinkedList to compare with. You should think of the solution using Stack or a TreeSet( if you have read about it). For the stack based solution, think of it in this way. An element is maximum of any subarray only when the next Maximum to its right lies beyond a distance of k. So start by calculating the next bigger element for each of the element and try proceeding further. If you don’t get it, ask me.

sorry i don’t get you.