Description

Given an array of integers nums and an integer k, return the median of each window of size k as the window slides from left to right.

Examples

Input:nums = [1,3,-1,-3,5,3,6,7], k = 3
Output:[1,-1,-1,3,5,6]
Explanation:

Window medians as it slides across the array.

Input:nums = [1], k = 3
Output:[1]
Explanation:

Edge case with a single-element array.

Input:nums = [2,4,1,6,8,3], k = 4
Output:[2.5,4,4.5]
Explanation:

With even window size k=4, medians are averages of middle two elements. Window [2,4,1,6] sorted is [1,2,4,6], median = (2+4)/2 = 2.5. Window [4,1,6,8] sorted is [1,4,6,8], median = (4+6)/2 = 4. Window [1,6,8,3] sorted is [1,3,6,8], median = (3+6)/2 = 4.5.

Constraints

  • 1 ≤ k ≤ nums.length ≤ 10⁵
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!