Greetings,
Any input on a way to divide a std::vector into two equal parts ? I need to find the smallest possible difference between |part1 – part2|.
This is how I’m doing it now, but from what you can probably tell it will yield a non-optimal split in some cases.
auto mid = std::find_if(prim, ultim, [&](double temp) -> bool
{
if(tempsum >= sum)
return true;
tempsum += temp;
sum -= temp;
return false;
});
The vector is sorted, highest to lowest, values can indeed appear twice.
I’m not expecting part1 and part2 to have the same numbers of elements, but sum(part1) should be as close as possible to sum(part2)
For example if we would have { 2.4, 0.12, 1.26, 0.51, 0.70 }, the best split would be { 2.4, 0.12 } and { 1.26, 0.51, 0.70 }.
If it helps, I’m trying to achieve the splitting algorithm for the Shannon Fano encoding.
Maybe this will help you guys understand my question better http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding#Example
Any input is appreciated, thanks!
Given that:
This is not optimal, but it will provide a reasonable approximation for values such as those you have given (unless I mucked something up … I didn’t actually compile or test it). It also works if you have negative numbers in the original vector:
EDIT: The quality of the result will be lower with this approach if there the sum of the negative numbers in your vector overwhelms the sum of the positive numbers in the list. To resolve, you could try to order the original list by descending absolute value rather than in strictly descending order.