FastPrepFastPrep
Problem Brief

All About Medians

NEW GRADOA
See Amazon online assessment and 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].

1Example 1

Input
nums = [1, 2, 3], k = 2
Output
[2, 1]
Explanation
Example 1 illustration
We can see that the max median is 2 and the min median is 1.

2Example 2

Input
nums = [56, 21], k = 1
Output
[56, 21]
Explanation
Example 2 illustration
Sometimes, an image tells all.. 🤫

3Example 3

Input
nums = [16, 21, 9, 2, 78], k = 5
Output
[16, 16]
Explanation
Example 3 illustration
A picture is worth a thousand words.. 🤧

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 10^5
  • 0 ≤ nums[i] ≤ 10^9
  • 1 ≤ k ≤ n
public int[] medians(int[] nums, int k) {
  // write your code here
}
Input

nums

[1, 2, 3]

k

2

Output

[2, 1]

Sign in to submit your solution.