Maximum sliding subarrays

Question

Suppose you're given an array of integers as well as a number k. You can assume that k is >=1 and <= the length of the array. Given these inputs, write a function to compute the maximum values of each subarray of length k. Avoid using built in max() functions.

For example, given...

array = [9, 4, 5, 3, 7, 8]
k = 3

Your function should return:

array = [9, 5, 7, 8]

Since:

9 = max(9,4,5)
5 = max(4,5,3)
7 = max(5,3,7)
8 = max(3,7,8)

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now