Sliding Window Median - Problem

The median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.

For example, if arr = [2, 3, 4], the median is 3.

For example, if arr = [1, 2, 3, 4], the median is (2 + 3) / 2 = 2.5.

You are given an integer array nums and an integer k. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves right by one position.

Return the median array for each window in the original array. Answers within 10⁻⁵ of the actual value will be accepted.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,3,-1,-3,5,3,6,7], k = 3
Output: [1.0,-1.0,-1.0,3.0,5.0,6.0]
💡 Note: Window [1,3,-1] → sorted [-1,1,3] → median 1.0. Window [3,-1,-3] → sorted [-3,-1,3] → median -1.0. Continue for all windows.
Example 2 — Even Window Size
$ Input: nums = [1,2,3,4], k = 2
Output: [1.5,2.5,3.5]
💡 Note: Window [1,2] → median (1+2)/2 = 1.5. Window [2,3] → median (2+3)/2 = 2.5. Window [3,4] → median (3+4)/2 = 3.5.
Example 3 — Single Element Window
$ Input: nums = [1,4,2,3], k = 1
Output: [1.0,4.0,2.0,3.0]
💡 Note: Each window contains one element, so median equals that element: 1.0, 4.0, 2.0, 3.0.

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ k ≤ nums.length
  • -231 ≤ nums[i] ≤ 231 - 1

Visualization

Tap to expand
Sliding Window Median INPUT nums array (k = 3) 1 3 -1 -3 5 3 6 7 0 1 2 3 4 5 6 7 Window slides right: [1,3,-1] med=1 [3,-1,-3] med=-1 [-1,-3,5] med=-1 Input Values nums = [1,3,-1,-3,5,3,6,7] k = 3 Window size: 3 elements ALGORITHM STEPS 1 Use Two Heaps Max-heap (left), Min-heap (right) Max Heap smaller half [-1, -3] Min Heap larger half [3, 5] 2 Insert Element Add to max-heap, balance heaps 3 Remove Outgoing Lazy deletion with hash map 4 Get Median Top of max-heap or avg of tops Median Calculation: If k is odd: median = maxHeap.top() If k is even: median = (max.top+min.top)/2 FINAL RESULT Medians for each window: Window Sorted Med [1,3,-1] [-1,1,3] 1.0 [3,-1,-3] [-3,-1,3] -1.0 [-1,-3,5] [-3,-1,5] -1.0 [-3,5,3] [-3,3,5] 3.0 [5,3,6] [3,5,6] 5.0 [3,6,7] [3,6,7] 6.0 Output Array: [1.0, -1.0, -1.0, 3.0, 5.0, 6.0] OK - 6 medians Time: O(n log k) | Space: O(k) Key Insight: Two heaps maintain a balanced partition: max-heap stores smaller half, min-heap stores larger half. The median is always accessible at heap tops. Lazy deletion handles element removal efficiently by tracking counts in a hash map and cleaning tops only when needed, achieving O(n log k) time. TutorialsPoint - Sliding Window Median | Optimal Solution (Two Heaps)
Asked in
Google 42 Facebook 38 Amazon 35 Microsoft 28
78.0K Views
Medium Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen