Solved Problem - Sliding Window Maximum
We'll cover the following
Problem Statement
Given an array , of integers. Print the maximum integer in that subarray for all subarrays of length .
Input format
The first line contains two space-separated integers and . The second line contains space separated integers representing the array
Output format
Print a single line containing integers—the maximum integer in each subarray of size from left to right.
Sample
Input
6 3
3 8 4 5 1 9
Output
8 8 5 9
Brute force
The brute force solution would simply use 2 for-loops. The first loop will determine the endpoint of the -sized subarray. Second loop to iterate through that subarray to find the maximum integer.
See the below code for a better understanding.
Get hands-on with 1400+ tech skills courses.