Is there an efficient algorithm to split up a number into N
subsections so that the sum of the numbers adds up to the original, with a base minimum? For example, if
here is a pseudo random solution [note that solution might be biased, but will be relatively random].
input:
n - the number we should sum up to
k - the number of 'parts'
m - minimum
(1) split n into k numbers: x1,x2,...,xk such that x1+...+xk = n, and the numbers
are closest possible to each other [in other words, x1 = x2 = ... = n/k where
possible, the end might vary at atmost +-1.]
(2) for each number xi from i=1 to k-1:
temp <- rand(m,xi)
spread x - temp evenly among xi+1,...,xk
xi <- temp
(3) shuffle the resulting list.
regarding part 1, for example: for n=50, k = 7
, you will set:
x1=x2=...=x6=7,x7=8
, no problem to compute and populate such a list with linear time.
Performance:
As said, step1 is O(k).
Step2, with naive implementation is O(k^2), but since you distribute result of temp-xi
evenly, there is O(k) implementation, with just storing and modifying delta.
Step3 is just a simple shuffle, O(k)
Overall performance: O(k) with delta implemntation of step2