Maximum of all subarrays of size k Given an array and an integer k, find the maximum for each and every contiguous subarray of size k.
Examples:
Input:
arr[] = {1, 2, 3, 1, 4, 5, 2, 3, 6}
k = 3

Output:
3 3 4 5 5 5 6

Input:
arr[] = {8, 5, 10, 7, 9, 4, 15, 12, 90, 13}
k = 4

Output:
10 10 10 15 15 90 90


Looking for code-review, best practices, and optimizations.
public class MaxOfSubArrays {

    private MaxOfSubArrays() { }
    

    public static List&lt;Integer&gt; maxList(int[] a, int k)   {
        if (k &lt;= 0) {
            throw new IllegalArgumentException(&quot;The value of k should atleast be 1.&quot;);
        }
        
        final Deque&lt;Integer&gt; deque = new ArrayDeque&lt;Integer&gt;();
        final List&lt;Integer&gt; result = new ArrayList&lt;Integer&gt;();
        
        int i;
        for (i = 0; i &lt; k; i++) {
            while (!deque.isEmpty() &amp;&amp; a[deque.peek()] &lt;= a[i]) {
                deque.removeFirst();
            }
            deque.addLast(i);
        }
        
        
        for (; i &lt; a.length; i++) {
            result.add(a[deque.peek()]);
            
            while (!deque.isEmpty() &amp;&amp; deque.peek() &lt;= i - k) {
                deque.removeFirst();
            }
            
            while (!deque.isEmpty() &amp;&amp; a[deque.peek()] &lt;= a[i]) {
                deque.removeFirst();
            }
            
            deque.addLast(i);
        }

        result.add(a[deque.peek()]);
        return result;
    }
}


public class MaxOfSubArraysTest {

    @Test
    public void test1() {
        int[] a1 = {1, 2, 3, 4, 5, 6};
        List&lt;Integer&gt; list1 = new ArrayList&lt;Integer&gt;(Arrays.asList(3, 4, 5, 6));
        assertTrue(list1.equals(MaxOfSubArrays.maxList(a1, 3)));
    }
    
    @Test
    public void test2() {
        int[] a2 = {6, 5, 4, 3, 2, 1};
        List&lt;Integer&gt; list2 = new ArrayList&lt;Integer&gt;(Arrays.asList(6, 5, 4, 3));
        assertTrue(list2.equals(MaxOfSubArrays.maxList(a2, 3)));
    }
    
}
Here you are using a few structures which I feel are not the right tool for the job.

Firstly, you are given an array of int, but you are returning a List&lt;Integer&gt;. I dislike List&lt;Integer&gt;, but even worse than using a List for primitives, is using a mixture of them. Be consistent!

The dueue is a confusing concept. It is worse because you use a for-loop in a non-traditional way, with the loop-variable declared outside the loop, and you reuse it later. You don't have any comments explaining how you use it.

At this point, I have looked through the requirements, I have figured out how I would do it. I can see that your strategy may have some advantages, if it does what I think it does, but it is complicated, and hard to read. So, I am giving up....

Code Review not successful, not commented enough, not enough background information.