find the overall average:
in our example:
(7 + 11 + 9 + 14 + 12 + 17)/6 = 11.667
find the total length:
(11-7) + (14-9) + (17-12) = 4 + 5 + 5 = 14;
find the new min/max;
14/2 = 7
11.667 - 7 = 4.667
11.667 + 7 = 18.667
you can round 'em
4.667 ~ 5
18.667 ~ 19
start from the min, creating the sections by the intervals
(5, (11-7)+5) = (5,9)
(9, (14-9)+9) = (9,14)
(14, (17-12)+14) = (14,19)
NOTE:
this method will not keep the elements as equal as possible to the originals, but will keep them as close as possible to the original considering their relative values (preserving the center)
EDIT:
if you want to keep the averages of all intervals as close as possible to the original, you can implement a mathematical solution.
our problem's input is:
a1=(a1,1, a1,2) , ... , an=(an,1,an,2)
we will define:
ai1 = a1,2-a1,1 // define the intervals
b1 = (d, d+ai1)
bn = (d + sum(ai1..ain-1), d + sum(ai1..ain) )
bi1 = b1,2-b1,1 // define the intervals
we need to find a 'd' such as:
s = sum( abs((a1,1+a1,2)/2 - (b1,1+b1,2)/2) )
min(s) is what we want
in our example:
a1 = (7,11), ai1 = 4, Aavg1 = 9
a2 = (9,14), ai2 = 5, Aavg2 = 11.5
a3 = (12,7), ai3 = 5, Aavg3 = 14.5
b1 = (d, d+4) Bavg1 = d+2
b2 = (d+4, d+9) Bavg2 = d+6.5
b3 = (d+9, d+14) Bavg3 = d+11.5
s = abs(9-(d+2)) + abs(11.5-(d+6.5)) + abs(14.5-(d+11.5)) = abs(7-d) + abs(5-d) + abs(3-d)
now calculcate the derivative to find min/max OR iterate over d to get a result. in our case you will need to iterate from 3 to 7
that should do the trick