Jobs
Interviews

Solve Running Median of K Elements using Ruby Language

Solve Running Median of K Elements using Ruby to enhance your skills with ruby coding practice , master coding concepts, and prepare for interviews with practical exercises and detailed solutions.

Running Median of K Elements

Difficulty : Hard

Categories :

  • Arrays

Given an array of integers and a positive integer K, return an array of K integers where each element represents the median of all previous elements including itself. For an odd number of integers, the median is the middle element after sorting. For an even number of integers, the median is the average of the two middle elements after sorting (round down to nearest integer).

Constraints:

  • K ≤ arr.length ≤ 10⁵
  • 1 ≤ K ≤ 10⁴
  • -10⁶ ≤ arr[i] ≤ 10⁶
  • The solution should run in O(K log K) time

Examples:

Input: arr = [1,4,3,2,6,5,8,7], K = 4
Output: [1,2,3,2]
Explanation:
First element: median of [1] is 1
Second element: median of [1,4] is 2
Third element: median of [1,4,3] is 3
Fourth element: median of [1,4,3,2] is 2
Input: arr = [5,2,8,1,9], K = 3
Output: [5,3,5]
Explanation:
First element: median of [5] is 5
Second element: median of [5,2] is 3
Third element: median of [5,2,8] is 5

Problem Solving

Input

What You'll Find Here

Real-World Scenarios Solve problems inspired by common Ruby use cases.

Step-by-Step Guidance Understand the core concepts of Ruby through clear explanations.

Practical Skills Prepare for real-world challenges with hands-on coding exercises.

Choose from the following categories