Back to course home
0% completed
Vote For New Content
Max of All Subarrays of Size 'k' (medium)
Problem Statement
Given an integer array arr
and an integer k
, return the result
list containing the maximum for each and every contiguous subarray of size k
.
In other words, result[i] = max(arr[0],..., arr[k])
, result[1] = max(arr[1],...arr[k+1])
, etc.
Examples
Example 1
- Input: arr =
[1, 2, 3, 1, 4, 5, 2, 3, 6]
, k =3
- Output:
[3, 3, 4, 5, 5, 5, 6]
- Description: Here, subarray
[1,2,3]
has maximum 3,[2,3,1]
has maximum 3,[3,1,4]
has maximum 4,[1,4,5]
has maximum 5,[4,5,2]
has maximum 5,[5,2,3]
has maximum 5, and[2,3,6]
has maximum 6.
Example 2
- Input: arr =
[8, 5, 10, 7, 9, 4, 15, 12, 90, 13]
, k =4
- Output:
[10, 10, 10, 15, 15, 90, 90]
- Description: Here, the maximum of each subarray of size 4 are 10, 10, 10, 15, 15, 90, 90 respectively.
Example 3
- Input: arr =
[12, 1, 78, 90, 57]
, k =3
- Output:
[78, 90, 90]
- Description: Here, the maximum of each subarray of size 3 are 78, 90, and 90 respectively.
Constraints:
- 1 <= arr.length <= 10<sup>5</sup>
- -10<sup>4</sup> <= arr[i] <= 10<sup>4</sup>
- 1 <= k <= arr.length
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
On this page
Problem Statement
Examples
Try it yourself