Median of Subarrays of Specific Size

Given an integer array ARR of size N and a specified subarray size M, calculate and return the median of all subarrays of size M starting from the left of the array.

The median is the middle value in an ordered integer array. If the size of the array is even, the median is the mean of the two middle numbers.

Input:

The first line contains a single integer ‘T’ representing the number of test cases.
For each test case:
The first line contains two space-separated integers ‘N’ and ‘M’, the size of the array ‘ARR’ and the subarray size, respectively.
The second line contains ‘N’ space-separated integers representing the elements of the array.

Output:

For each test case, return the medians of all subarrays with size ‘M’.

Example:

Input:
ARR = [1, 4, 3, 5], M = 3
Output:
[3.0, 4.0]
Explanation:

The first subarray of size 3 is [1, 4, 3] with a median of 3. The second subarray is [4, 3, 5] with a median of 4. Thus, the medians are [3.0, 4.0].

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 1000
  • 1 <= M <= N
  • 1 <= ARR[i] <= 10^6

Time Limit: 1 second

AnswerBot
9d

Calculate and return the median of all subarrays of a specified size in an integer array.

  • Iterate through the array and for each subarray of size M, calculate the median.

  • If the size of the subarray is ...read more

Help your peers!
Add answer anonymously...
Flipkart Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter