0% found this document useful (0 votes)
48 views79 pages

Finding Maximum of All Subarrays of Size K in The Given Array

The document describes a naive algorithm to find the maximum of all sub-arrays of size K in a given input array. It works as follows: 1) Iterate through the input array from index 0 to n-K. 2) For each index i, find the maximum of the sub-array from i to i+K-1. This involves comparing the element at i to the next K-1 elements. 3) Output the maximum of each sub-array. The time complexity is O(n*K) as it involves comparing each element to K-1 other elements.

Uploaded by

Ansh Gulati ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views79 pages

Finding Maximum of All Subarrays of Size K in The Given Array

The document describes a naive algorithm to find the maximum of all sub-arrays of size K in a given input array. It works as follows: 1) Iterate through the input array from index 0 to n-K. 2) For each index i, find the maximum of the sub-array from i to i+K-1. This involves comparing the element at i to the next K-1 elements. 3) Output the maximum of each sub-array. The time complexity is O(n*K) as it involves comparing each element to K-1 other elements.

Uploaded by

Ansh Gulati ji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 79

Finding maximum of all sub-

arrays of size K in the given


array
What do we have?
The input array of size (n = 8)

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

max(3,9,-1) max(-1,14,15) max(15,1,2)


max(12,3,9) max(9,-1,14) max(14,15,1)
Naive Approach
Go to each element and find maximum for a sub-array starting from that
element (you only need to go from 0 to (n-K) (because after that you cannot
make a sub-array of size K)

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

And we stop here. . . . .

Because taking i = 6 will need a comparison with index


7 and 8 (but we only have indices up to 7)

Output : 12 9 14 15 15 15
Naive Approach

Time Complexity = O (n.k)


- We have done k-1 (=2) comparisons for each of
(n-k + 1 elements or 6 elements). So we have (n-
k+1)x(k-1) comparisons
- And (n-k+1)(k-1) will have nk as the maximum
value term. . .Thus, O(nk)

Space Complexity = O(1) [only limited variables were


required, like i, j, max, etc. and these don’t depend on
input size n]
Using Deque (A sliding window)

Time Complexity = O (n.k)


- But we have made redundant comparisons
- We can minimize time complexity by using a
Deque. We will only use an element once (or twice)
for comparison when we ENQUEUE (or DEQUEUE)
it in (or from) the Deque.
Using Deque (A sliding window)

What is a Deque? (usually pronounced “Deck”)

- A Double Ended Queue that allows insertions and deletions


on any side (but removal of elements at the corners is
allowed, you cannot delete an element from the middle of
the queue)
A queue
A Deque
What do we have?
The input array of size (n = 8)

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

max(3,9,-1) max(-1,14,15) max(15,1,2)


max(12,3,9) max(9,-1,14) max(14,15,1)
Using Deque (A sliding window)
We will maintain a Deque that has INDEX of largest element on the
FRONT(left hand side) and INDEX of elements on the right hand side(REAR)
are such that array elements at those indices are in decreasing order

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

i = 0 (Current element) Window (or deque) is


empty.
⇒ Insert the index of
current 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

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)

0 2 Move to the next


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

Show the element at front-index as it is


MAXIMUM of the previous window. . . .
i = 3 (Current element)
Peek First = 0
Element at Index 0 = 12
0 2
Output : 12
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

Remove indices which are not in the


Current window
current window. . . .(from FRONT)
i = 3 (Current element)
The current window is the current element
and previous k-1 elements.
So indices less than equal to (i-k) are out
0 2 of the current window

⇒ Remove 0 (from FRONT)


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 Remove indices which are not in the


current window. . . .(from FRONT)
i = 3 (Current element)
The current window is the current element
and previous k-1 elements.
2 So indices less than equal to (i-k) are out
of the current window
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 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

2 Peek No, element at rear-index (2) is 9 which is


Last greater than -1
⇒ So, we can simply insert index of -1
from rear
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)

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)

⇒ Now move to the next 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 Show the element at front-index as


it is MAXIMUM of the previous
i = 4 (Current element) window. . . .

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

Current window Remove indices which are not in


the current window. . . .(from
FRONT)
i = 4 (Current element)
The current window is the current
element and previous k-1
2 3 elements.
So indices less than equal to (i-k)
are out of the current window
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 The current window is the current


element and previous k-1
elements.
i = 4 (Current element)
So, indices less than equal to (i-k)
are out of the current window

2 3 No element is out of current


window
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 Now look from the rear end of


the queue to see if ELEMENTS @
i = 4 (Current element) INDEX at rear are LESS THAN
the current element

2 3 Peek Element at rear-index (3) is -1


Last which is LESS THAN 14
⇒ So, remove from last
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)

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

Current window Now look from the rear end of


the queue to see if ELEMENTS @
i = 4 (Current element) INDEX at rear are LESS THAN
the current element

2 Peek Element at rear-index (2) is 9


Last which is LESS THAN 14
⇒ So, remove from last
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 Now deque is EMPTY

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)

4 Move to next 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

Show the element at front- Current window


index as it is MAXIMUM of the
previous window. . . . i = 5 (Current element)

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

Removing index which are Current window


out of the current window.
i = 5 (Current element)
...

No index gets removed as


the index-at-front is within 4
the current window. . .
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

Element at index 4 (looked Current window


from rear) is LESS THAN
i = 5 (Current element)
current element (15). . .
⇒ Remove the index
from the deque (from Peek
4
rear side) Last
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 = 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

Deque is empty. . . Current window


⇒ Insert INDEX OF i = 5 (Current element)
current element. . .
from rear end
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 = 5 (Current element)

Move to the next


5
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

Show the element at front- Current window


index as it is MAXIMUM of the
previous window. . . . i = 6 (Current element)

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

Remove indices that are out of Current window


current window. . .
i = 6 (Current element)
No index is removed from the
front end as index at the front
is IN THE CURRENT
WINDOW. . . 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

Element at index 5 (15) is Current window


GREATER THAN current
i = 6 (Current element)
element (1). . .No removal
⇒ Insert the index of
current element from Peek
5
rear end. . . Last
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)

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

Show the element at front- Current


window
index as it is MAXIMUM of the
previous window. . . . i = 7 (Current element)

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

Remove indices from Current


window
front, which are out of
i = 7 (Current element)
current window. .
⇒ No removal. . .
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

Remove indices from rear Current


window
which have elements
i = 7 (Current element)
LESS THAN current
element.
Element at 6 (1) is less
than current element (2) 5 6
⇒ Remove 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 = 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

Remove indices from rear which Current


have elements LESS THAN current window
element. i = 7 (Current element)
Element at 5 (15) is NOT less than
current element (2)
⇒ Stop removing and add 5
current index
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 = 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)

So, before printing the maximum of LAST window. . .the loop


ends. ..

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 the deque to get the maximum


of last window. . .

Peek first = 5
Element at 5 = 15
5 2
Output : 12 9 14 15 15 15
Using Deque (A sliding window)

Time Complexity = O (n)


- It looks like O(nk) but it is not, because elements
are being added to the deque only once and removed
only once (NO REDUNDANT COMPARISONS)
- So, there are just 2 operations on each element,
insertion and deletion to the deque.
- Total we have 2n operations or O(n)
Space Complexity = O(K) [A deque of size K will be
used]

You might also like