Problem 126953 · hard · Level 01 Prerequisites & Setup

A Dataset to Order

median · minimum and maximum · sums · constructing examples

A statistics teacher wants practice datasets with given summaries, so that pupils can check their answers. Each dataset has an odd number n of whole numbers, and the teacher asks for:

  • the smallest value to be exactly low, and the largest exactly high,
  • the median (the middle value once the list is sorted) to be exactly middle,
  • the values to add up to exactly total (so the mean is total / n).

Write invent(n, low, high, middle, total) that returns such a list of n whole numbers, in any order, or None if no such list exists. When there are many possible lists, any one of them is accepted.

Examples

Input:  n = 5, low = 1, high = 9, middle = 4, total = 25
Output: [1, 4, 4, 7, 9]   (one possible answer; [1, 3, 4, 8, 9] is another)
Explanation: sorted, the middle value is 4; the smallest is 1, the largest 9, and the sum 25.

Input:  n = 5, low = 1, high = 9, middle = 4, total = 40
Output: None
Explanation: the two values above the middle can be at most 9 each and the one below it
at most 4, so the largest possible total is 1 + 4 + 4 + 9 + 9 = 27.

Input:  n = 3, low = 2, high = 8, middle = 5, total = 15
Output: [2, 5, 8]

Constraints

  • n is odd, 1 <= n <= 99999
  • 0 <= low <= middle <= high <= 10**6 and 0 <= total <= 10**12

How your answer is checked

A list is accepted when it has exactly n whole numbers (ints), its minimum is low, its maximum is high, its sorted middle element is middle, and its sum is total. None is accepted only when no such list exists.

Goals

  • Work out which totals are possible once the smallest, largest and middle values are fixed
  • Build a list that meets several summaries at once
  • Prove that a request is impossible instead of searching blindly
Starting Python…