1349. Pivot of Fives

A tournament organiser picks a seeding pivot from the strength ratings in nums with a group-of-five rule. If the list has at most 5 entries, the pivot is its lower median: the entry at index (m - 1) / 2, rounded down, of the sorted list of length m. Otherwise, cut the list into consecutive groups of 5 in the given order (the last group may be shorter), take the lower median of each group, collect these medians in group order into a new list, and apply the same rule to that new list.

Return the pivot that the rule finally produces. The pivot need not be the true median of nums. The list shrinks by a factor of 5 each round, so the work is O(n) overall.

Example 1

Input:
nums = [9,1,8,2,7,3,6]
Output:
3
Explanation:

The groups [9, 1, 8, 2, 7] and [3, 6] have lower medians 7 and 3. The list [7, 3] has size at most 5, so its lower median 3 is returned.

Example 2

Input:
nums = [5,4,3,2]
Output:
3
Explanation:

With 4 elements the lower median is at sorted index 1, the value 3.

Example 3

Input:
nums = [1,2,3,4,5,6,7,8,9,10]
Output:
3
Explanation:

The groups [1..5] and [6..10] give medians 3 and 8, and the lower median of [3, 8] is 3.

Constraints

  • 1 ≤ n ≤ 1000, where n is the length of nums
  • -109 ≤ nums[i] ≤ 109

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…