FastPrepSliding Window Maximum

Sliding Window Maximum

Groww logoGroww● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums and a window size k, return the maximum value in every contiguous window of length k, from left to right.

Function

maxSlidingWindow(nums: int[], k: int) → int[]

Examples

Example 1

nums = [1,3,-1,-3,5,3,6,7]k = 3return = [3,3,5,5,6,7]

Each output is the largest value in the corresponding length-three window.

Example 2

nums = [1]k = 1return = [1]

The only window contains the only value.

Example 3

nums = [9,8,7,6]k = 2return = [9,8,7]

In a decreasing array, each window maximum is its leftmost value.

Constraints

  • 1 <= nums.length <= 100000
  • -1000000000 <= nums[i] <= 1000000000
  • 1 <= k <= nums.length

More Groww problems

See Groww hiring insights
public int[] maxSlidingWindow(int[] nums, int k) {
    // Write your code here.
}
nums[1,3,-1,-3,5,3,6,7]
k3
expected[3,3,5,5,6,7]
Checking account…