public static void printMaxOfKElements(int [] a, int k) {
    ArrayDeque<Integer> q = new ArrayDeque<Integer>();  
     
    for(int i = 0 ; i < k; i++) {
      while(!q.isEmpty() && a[q.getLast()] <= a[i]) {
        q.pollLast();
      }
      q.addLast(i);
    }
    
    for(int i = k; i < a.length; i++) {
      System.out.println(a[q.getFirst()]);
      
      while(!q.isEmpty() && q.getFirst() <= i-k) {
        q.pollFirst();
      }
      
      while(!q.isEmpty() && a[q.getLast()] <= a[i]){
        q.pollLast();
      }
      q.addLast(i);
    }
    System.out.println(a[q.getFirst()]);
  }