Finding Maximum of All Subarrays of Size K in The Given Array
Finding Maximum of All Subarrays of Size K in The Given Array
12 3 9 -1 14 15 1 2
Value of variable K = 3
What do we want?
Output
12 9 14 15 15 15
The output explained
The input array of size n (=8)
12 3 9 -1 14 15 1 2
Value of variable K = 3
Output
12 9 14 15 15 15
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Naive Approach
Take the current element to be maximum
(initially) then compare it with next (k-1) elements
or 3-1 = 2 elements. . . .
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 0 (Current element)
Max = 12
(initially)
Naive Approach
Take the current element to be maximum
Therefore we have to compare element at
(initially) then compare it with next (k-1) elements
i=0 with elements at index 1 and 2
or 3-1 = 2 elements. . . .
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=1
i = 0 (Current element)
Max = 12
Naive Approach
Take the current element to be maximum
Therefore we have to compare element at
(initially) then compare it with next (k-1) elements
i=0 with elements at index 1 and 2
or 3-1 = 2 elements. . . .
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=2
i = 0 (Current element)
Max = 12
Output : 12
Naive Approach
Take the current element to be maximum
Therefore we have to compare element at
(initially) then compare it with next (k-1) elements
i=0 with elements at index 1 and 2
or 3-1 = 2 elements. . . .
12 3 9 -1 14 15 1 2
i = 0 (Current element)
Max = 12 (after comparing with other two elements in the current sub-array of size k=3)
Naive Approach
Move to the next element. . . (i=1)
Therefore we have to compare element at
Compare it with next k-1 elements
i=1 with elements at index 2 and 3
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 1 (Current element)
Max = 3 (initially)
Naive Approach
Move to the next element. . . (i=1)
Therefore we have to compare element at
Compare it with next k-1 elements
i=1 with elements at index 2 and 3
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=2
i = 1 (Current element)
Max = 9
Naive Approach
Move to the next element. . . (i=1)
Therefore we have to compare element at
Compare it with next k-1 elements
i=1 with elements at index 2 and 3
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=3
i = 1 (Current element)
Max = 9
Output : 12 9
Naive Approach
Move to the next element. . . (i=1)
Therefore we have to compare element at
Compare it with next k-1 elements
i=1 with elements at index 2 and 3
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 1 (Current element)
Max = 9 (after comparing with other two elements in the current sub-array of size k=3)
Naive Approach
Move to the next element. . . (i=2)
Therefore we have to compare element at
Compare it with next k-1 elements
i=2 with elements at index 3 and 4
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 2 (Current element)
Max = 9 (Initially)
Naive Approach
Move to the next element. . . (i=2)
Therefore we have to compare element at
Compare it with next k-1 elements
i=2 with elements at index 3 and 4
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=3
i = 2 (Current element)
Max = 9 (after
comparison)
Naive Approach
Move to the next element. . . (i=2)
Therefore we have to compare element at
Compare it with next k-1 elements
i=2 with elements at index 3 and 4
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=4
i = 2 (Current element)
Max = 14 (after
comparison)
Output : 12 9 14
Naive Approach
Move to the next element. . . (i=2)
Therefore we have to compare element at
Compare it with next k-1 elements
i=2 with elements at index 3 and 4
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 2 (Current element)
Max = 14 (after comparing with other two elements in the current sub-array of size k=3)
Naive Approach
Move to the next element. . . (i=3)
Therefore we have to compare element at
Compare it with next k-1 elements
i=3 with elements at index 4 and 5
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 3 (Current element)
Max = -1 (Initially)
Naive Approach
Move to the next element. . . (i=3)
Therefore we have to compare element at
Compare it with next k-1 elements
i=3 with elements at index 4 and 5
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=4
i = 3 (Current element)
Max = 14
Naive Approach
Move to the next element. . . (i=3)
Therefore we have to compare element at
Compare it with next k-1 elements
i=3 with elements at index 4 and 5
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=5
i = 3 (Current element)
Max = 15
Output : 12 9 14 15
Naive Approach
Move to the next element. . . (i=3)
Therefore we have to compare element at
Compare it with next k-1 elements
i=3 with elements at index 4 and 5
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 3 (Current element)
Max = 15 (after comparing with other two elements in the current sub-array of size k=3)
Naive Approach
Move to the next element. . . (i=4)
Therefore we have to compare element at
Compare it with next k-1 elements
i=4 with elements at index 5 and 6
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 4 (Current element)
Max = 14 (Initially)
Naive Approach
Move to the next element. . . (i=4)
Therefore we have to compare element at
Compare it with next k-1 elements
i=4 with elements at index 5 and 6
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=5
i = 4 (Current element)
Max = 15
Naive Approach
Move to the next element. . . (i=4)
Therefore we have to compare element at
Compare it with next k-1 elements
i=4 with elements at index 5 and 6
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=6
i = 4 (Current element)
Max = 15
Output : 12 9 14 15 15
Naive Approach
Move to the next element. . . (i=4)
Therefore we have to compare element at
Compare it with next k-1 elements
i=4 with elements at index 5 and 6
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 4 (Current element)
Max = 15 (after comparing with other two elements in the current sub-array of size k=3)
Naive Approach
Move to the next element. . . (i=5)
Therefore we have to compare element at
Compare it with next k-1 elements
i=5 with elements at index 6 and 7
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 5 (Current element)
Max = 15 (Initially)
Naive Approach
Move to the next element. . . (i=5)
Therefore we have to compare element at
Compare it with next k-1 elements
i=5 with elements at index 6 and 7
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=6
i = 5 (Current element)
Max = 15
Naive Approach
Move to the next element. . . (i=5)
Therefore we have to compare element at
Compare it with next k-1 elements
i=5 with elements at index 6 and 7
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
j=7
i = 5 (Current element)
Max = 15
Output : 12 9 14 15 15 15
Naive Approach
Move to the next element. . . (i=5)
Therefore we have to compare element at
Compare it with next k-1 elements
i=5 with elements at index 6 and 7
(k-1 = 2 elements)
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 5 (Current element)
Max = 15 (after comparing with other two elements in the current sub-array of size k=3)
Naive Approach
Output : 12 9 14 15 15 15
Naive Approach
12 3 9 -1 14 15 1 2
Value of variable K = 3
What do we want?
Output
12 9 14 15 15 15
The output explained
The input array of size n (=8)
12 3 9 -1 14 15 1 2
Value of variable K = 3
Output
12 9 14 15 15 15
Deque will have at most K elements [an element is moved out of the deque if
it is not the part of the current window]
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Using Deque (A sliding window)
The deque has following properties:
- Contains indices (to keep track of elements that are
in the current window)
- Front (Left side) has index of LARGEST ELEMENT in
the CURRENT WINDOW
- Index of elements are arranged from front to rear
(left to right) in decreasing order of the elements at
those indices
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Using Deque (A sliding window)
The algorithm
- Process first window (or first K elements in the array) [to set the Deque up]
- Then process other elements
- Print the element at INDEX at the front of the deque (because it is the largest element of
PREVIOUS WINDOW)
- Remove elements that are out of current window (dequeue from Front or left hand side)
- Attempt to insert current element from REAR of the DEQUE (right hand side)
- If the deque has an index on the rear end where element is greater than the current
element => ENQUEUE from rear
- Otherwise UNTIL deque has an index where element less than or equal to current
element, remove the elements(from rear) till the queue is empty or a greater element
is found
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
i = 0 (Current element)
Move to the next element
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Current element 3 is
smaller than element @
i = 1 (Current element) last-inserted index in the
Peek
deque (12). . .
0 Last
⇒ Insert index of 3
from Rear
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 1 (Current element)
Move to the next element
0 1
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Current element 9 is
i = 2 (Current element)
greater than element @
0 1 Peek index (at rear) in the
Last deque (3). . .
⇒ Remove from deque
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 2 (Current element)
0
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Current element 9 is
i = 2 (Current element) LESS than element @
index (as seen from rear)
Peek
0 Last in the deque (12). . .
⇒ We insert index of 9
Using Deque (A sliding window) - The example
Process the first K=3 elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
i = 2 (Current element)
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current window Now look from the rear end of the queue
to see if ELEMENTS @ INDEX at rear are
i = 3 (Current element) LESS THAN or equal to the current
element
12 3 9 -1 14 15 1 2
Current window
i = 3 (Current element)
2 3
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Current window
i = 3 (Current element)
12 3 9 -1 14 15 1 2
Peek First = 2
2 3 Element at Index 2 = 9
Output : 12 9
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current window
i = 4 (Current element)
2
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
i = 4 (Current element)
⇒Insert the current element from
rear side
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Current window
i = 4 (Current element)
12 3 9 -1 14 15 1 2
Peek First = 4
Element at Index 4 = 14
4
Output : 12 9 14
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current window
i = 5 (Current element)
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current window
i = 5 (Current element)
12 3 9 -1 14 15 1 2
Peek First = 5
Element at Index 5 = 15
5
Output : 12 9 14 15
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current window
i = 6 (Current element)
5 6
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Current window
i = 6 (Current element)
Now move to the next
element. . .
5 6
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Peek First = 5
Element at Index 5 = 15
5 6
Output : 12 9 14 15 15
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current
window
i = 7 (Current element)
5
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
12 3 9 -1 14 15 1 2
Current
window
i = 7 (Current element)
5 2
Using Deque (A sliding window) - The example
AT THIS POINT (in our code) the LOOP ENDS. . . as “i” becomes 8
and is not less than 7 (length of the array - 1)
Thus, we peek the deque after the loop to print the last element
Using Deque (A sliding window) - The example
Process other elements
0 1 2 3 4 5 6 7
12 3 9 -1 14 15 1 2
Peek first = 5
Element at 5 = 15
5 2
Output : 12 9 14 15 15 15
Using Deque (A sliding window)