Problem · Array

All About Medians

EasyAmazonNEW GRADOA
See Amazon hiring insights

A new Amazon intern encountered a challenging task. Currently, the intern has n integers, where the value of the ith element is represented by the array element values[i]. The intern is curious to play with arrays and subsequences and thus asks you to join him. Given an integer, array values, and an integer k, the intern needs to find the maximum and minimum median overall subsequences of length k.

Function Description

Complete the function medians in the editor below.

medians has the following parameter(s):

  1. int values[n]: the value of integers.
  2. int k: the given integer

Returns

int[]: the maximum and minimum overall subsequences of length k in the form [maximum median, minimum median].

Examples
01 · Example 1
nums = [1, 2, 3]
k = 2
return = [2, 1]
Example 1 illustration
We can see that the max median is 2 and the min median is 1.
02 · Example 2
nums = [56, 21]
k = 1
return = [56, 21]
Example 2 illustration
Sometimes, an image tells all.. 🤫
03 · Example 3
nums = [16, 21, 9, 2, 78]
k = 5
return = [16, 16]
Example 3 illustration
A picture is worth a thousand words.. 🤧
Constraints
  • 1 ≤ n ≤ 10^5
  • 0 ≤ nums[i] ≤ 10^9
  • 1 ≤ k ≤ n
More Amazon problems
drafts saved locally
public int[] medians(int[] nums, int k) {
  // write your code here
}
nums[1, 2, 3]
k2
expected[2, 1]
sign in to submit