I ran into a problem when I want to write an algorithm that can return the maximum element of each consecutive submatrix of k elements in a larger array, and these maximum elements are read into their own array, for example:
Given int array = {3, 7, 20, 6, 12, 2, 0, 99, 5, 16}, and int k = 4,
--> creates the array {20, 20, 20, 12, 99, 99, 99}
[because there are 7 consecutive sub-arrays of size 4 within the given array:
{3, 7, 20, 6}, {7, 20, 6, 12}, {20, 6, 12, 2}, ... , {0, 99, 5, 16}
and the max element of these, respectively, is 20, 20, 20, ..., 99 which
are read into the resulting array.
Now here is my problem: I know how to implement this in O (n ^ 2) complexity, but I want to make it faster so that it can be O (n), or if this is not possible, O (nlog (n)). Does anyone know if there is a faster way to do this, and if so, how?