mostvisited has asked for the
wisdom of the Perl Monks concerning the following question:

I need to divide an array of size n in two halves of size i and j such that |i-j| is 0 or 1 and the difference of the sums of two arrays is minimal.
e.g
for an array {9,1,1,1,1,1,1,1,1,1} the two partitions are
{9,1,1,1,1} and {1,1,1,1,1}

The first partition now sums to 1 and the second to -3 for a difference of 4. I don't know the difficulty in accounting for this situation but the problem is assuming the only balancing operation is swapping.

For the universe of positive integers, I sensed a simple solution lurking. I found one but I didn't like how it
coded up: It was just pushing the largest onto a half,
if it was forced onto the currently smaller half, put it
in a protest queue in that half. When ready to add a number to the other half in the presence of a protest, you would instead pop the top of the queue to the other half. This works and is fairly efficient but it is messy in the control code.

Today, it just struck me that you don't have to keep the halves abs( @left - @right) <= 1 as you create them;
just make sure there is enough remaining to fill the other half.

On the matter of dividing up items so that the difference in sum is minimized, you might be interested in this thread Average Price Algorithm. It discusses averages rather than sums and allows for an arbitrary number of buckets, instead of just 2. However, some of the issues will be the same since an average is just a sum divided by the number of elements. There were several suggestions on that thread ranging choosing the best from a random selection of solutions to various techniques for using deviation from the mean. Using deviations from the mean lets one take advantage of the fact that anything that pushes bucket's sum up so that its average is above the average for all elements, must, if possible, be complemented by something below the average for all elements. If you read through the thread discussing that solution and study the counter examples, you will see that its success is sensitive to relative factorizations, but that issue should be easily resolvable for the special case of two buckets.

On the matter of dividing the number of elements into two groups as evenly as possible, you will have to take into account that odd numbers can't be divided evenly by two. There are two ways to deal with this in Perl: one using integers and one using floating point:

Option 1: use floating point division and strip away fractional portion using int:

Option 2: Use the mod operator % (see perlop) to determine whether the size of the array is odd or even. $n%2 returns the remainder of division by 2.

# $n%2 will be 1 if $n is odd and 0 if $n is even.
my $n=scalar(@aSomeNumbers);
my ($n1, $n2);
if ($n%2) {
#if $n is even it is safe to divide by 2
$n1=$n2=$n/2;
} else {
#add one so we get an even number again and can divide safely by 2
$n1=($n+1)/2;
$n2=$n-$n1;
}

this code would simply divide the array.. but it doesnt take care of the sum condition. As i have mentioned the difference of sum of elements of two new arrays should be minimal. The new partitions should be balanced also. How to maintain that property ?

I am not directly acquainted with this problem, but it appears to me that the constraint, “such that |i-j| is 0 or 1,” is merely a formal way of saying, “in half.” Off the top of my head, I think that I can assert that there is only one point in any list where such a constraint would be true in a list with an even number of elements, and only two such points with an odd number.

Therefore, “how is the ‘minimize’ constraint to be satisfied?” It would seem obvious to me that you must alter the order of the elements of the list in some way... a task that could be equally described in terms of “two separate lists.” With such a definition, one might solve the problem by sorting the master-list, then distributing the numbers taken from both the head and the tail of that sorted master-list into the two buckets.

But, again referring to my previous post, I would first “study the literature” to find where someone else has effectively solved the problem for you already.
“Heck, it's got to be in CPAN somewhere...”

Yes! ...if you can find it anymore. How To Solve It, by Zbignie Michalewicz. A recent re-publication (co-authored by David B. Fogel) is more readily available, ISBN 978-3540224945.

This is a particularly-readable example of a concentrated book of heuristics. (Not just “algorithms,” and there is a difference.)
It shows you how to approach many practical problems whose solutions are not immediately obvious.

Vivek
-- In accordance with the prarabdha of each, the One whose function it is to ordain makes each to act. What will not happen will never happen, whatever effort one may put forth. And what will happen will not fail to happen, however much one may seek to prevent it. This is certain. The part of wisdom therefore is to stay quiet.

When I realized that this wasn't going to be quick and easy,
I decided that having a brute force implementation upon which to compare was necessary. Now, I think there is nothing better than to take a drink and a long walk.

It doesn't seem that anyone has provided a correct solution.
I believe this is one.

Went to join the gridlock to see it
Held an eclipse party
Watched a live feed
I cn"t see tge kwubosd to amswr thus
I tried to see it, but 8000 miles of rock got in the way
What eclipse?
Wanted to see it, but they wouldn't reschedule it
Read the book instead