Consider an array A
of length n
. The array contains only positive integers. For example A = (1,1,2,2)
. Let us define f(A)
as the set of sums of all the non-empty contiguous subarrays of A
. In this case f(A) = {1,2,3,4,5,6}
. The steps to produce f(A)
are as follows:
The subarrays of A
are (1), (1), (2), (2), (1,1), (1,2), (2,2), (1,1,2), (1,2,2), (1,1,2,2)
. Their respective sums are 1,1,2,2,2,3,4,4,5,6
. The set you get from this list is therefore {1,2,3,4,5,6}
.
Task
Given a set of sums S
given in sorted order containing only positive integers and an array length n
, your task is to output at least one array X
such that f(X) = S
.
For example, if S = {1,2,3,5,6}
and n = 3
then a valid output is X = (1,2,3)
.
If there is no such array X
your code should output any constant value.
Examples
Input: n=4, S = (1, 3, 4, 5, 6, 8, 9, 10, 13)
, possible output: X = (3, 5, 1, 4)
Input: n=6, S = (2, 3, 4, 5, 7, 8, 9, 10, 12, 14, 17, 22)
, possible output: X = (5, 3, 2, 2, 5, 5)
Input: n=6, S = (2, 4, 6, 8, 10, 12, 16)
, possible output: X = (4, 2, 2, 2, 2, 4)
Input: n=6, S = (1, 2, 3, 4, 6, 7, 8, 10, 14)
, possible output: X = (4, 2, 1, 1, 2, 4)
Input: n=10, S = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 23, 24, 25)
, possible output: X = (1, 1, 3, 1, 2, 1, 2, 5, 4, 5)
.
Input: n=15, S = (1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31)
, possible output: X = (1, 2, 1, 3, 3, 1, 3, 3, 1, 3, 3, 1, 2, 1, 3)
.
Input and output format
Your code can take input and give output in any easily human read format you find convenient. However, please show the output of testing it on the examples in the question.
Running time
You must be able to run the code to completion for all the examples in the question. It should in principle be correct for n
up to 15
but you do not need to prove it would be fast enough for all inputs.