Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

37. Sliding Window Maximum

HardDay 12 of 60Sliding Window

You are given an integer array nums and a window size k. A window of k consecutive elements slides from the left end of the array to the right end, one position at a time. Return an array with the maximum value inside the window at each position.

Example 1
Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [3,3,5,5,6,7]
The windows are [1,3,-1], [3,-1,-3], [-1,-3,5], [-3,5,3], [5,3,6], [3,6,7].
Example 2
Input: nums = [1], k = 1
Output: [1]
Constraints
  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length
Hints
Frequently asked at: Amazon, Google, Microsoft, Flipkart, D. E. Shaw
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min