My question is about a CodeFu practice problem (2012 round 2 problem 3). It basically comes down to splitting an array of integers in two (almost) equal halves and returning the smallest possible difference between the two. I have included the problem description below. As noted in the comments this can be described as a balanced partition problem, which is a problem in the realm of dynamic programming.
Now similar problems have been discussed a lot, but I was unable find an efficient solution for this particular one. The problem is of course that the number of possible combinations to traverse soon grows too large for a brute force search (at least when using recursion). I have a recursive solution that works fine for all but the largest problem sets. I tried to add some optimizations that stop the recursion early, but the performance is still too slow to solve some arrays of the maximum length (30) within the 5 second maximum allowed by CodeFu. Any suggestions for how to improve or rewrite the code would be very welcome. I would also love to know if it might help to make the iterative version.
Update: on this fine site there is a theoretical discussion of the balanced partition problem, which gives a good idea of how to go about and solve this in a dynamic way. That is really what I am after, but I do not know how to put the theory into practice exactly. The movie mentions that the elements in the two subcollections can be found “using the old trick of back pointers”, but I don’t see how.
Problem
You and your friend have a number of coins with various amounts. You
need to split the coins in two groups so that the difference between
those groups in minimal.E.g. Coins of sizes 1,1,1,3,5,10,18 can be split as: 1,1,1,3,5 and
10,18 1,1,1,3,5,10 and 18 or 1,1,3,5,10 and 1,18 The third
combination is favorable as in that case the difference between the
groups is only 1. Constraints: coins will have between 2 and 30
elements inclusive each element of coins will be between 1 and
100000 inclusiveReturn value: Minimal difference possible when coins are split into
two groups
NOTE: the CodeFu rules state that the execution time on CodeFu’s server may be no more than 5 seconds.
Main Code
Arrays.sort(coins);
lower = Arrays.copyOfRange(coins, 0,coins.length-1);
//(after sorting) put the largest element in upper
upper = Arrays.copyOfRange(coins, coins.length-1,coins.length);
smallestDifference = Math.abs(arraySum(upper) - arraySum(lower));
return findSmallestDifference(lower, upper, arraySum(lower), arraySum(upper), smallestDifference);
Recursion Code
private int findSmallestDifference (int[] lower, int[] upper, int lowerSum, int upperSum, int smallestDifference) {
int[] newUpper = null, newLower = null;
int currentDifference = Math.abs(upperSum-lowerSum);
if (currentDifference < smallestDifference) {
smallestDifference = currentDifference;
}
if (lowerSum < upperSum || lower.length < upper.length || lower[0] > currentDifference
|| lower[lower.length-1] > currentDifference
|| lower[lower.length-1] < upper[0]/lower.length) {
return smallestDifference;
}
for (int i = lower.length-1; i >= 0 && smallestDifference > 0; i--) {
newUpper = addElement(upper, lower[i]);
newLower = removeElementAt(lower, i);
smallestDifference = findSmallestDifference(newLower, newUpper,
lowerSum - lower[i], upperSum + lower [i], smallestDifference);
}
return smallestDifference;
}
Data Set
Here is an example of a set that takes too long to solve.
{100000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000,60000,60000,60000,60000,60000,60000,
60000,60000,60000}
If you would like the entire source code, I have put it on Ideone.
Say
Nis the sum of all coins. We need to find a subset of coins, where the sum of its coins is closest toN/2. Let’s calculate all possible sums and choose the best one. In worst case we may expect 2^30 possible sums, but this may not happen, because the largest possible sum is 100K*30, that is 3M – much less than 2^30 which would be about 1G. So an array of 3M ints or 3M bits should be sufficient to hold all possible sums.So we have array
aanda[m] == 1if and only ifmis a possible sum.We start from zeroed array and have
a[0]=1, because the sum0is possible (one has no coins).When you finish in 30 * 3M steps you will know all possible sums. Find the number
mthat is closest toN/2. Your result isabs(N-m - m). I hope I fit in time and memory bounds.Edit: A correction is needed and 2 optimizations:
N+1(including 0), to solve smaller coin sets faster.mandN-m, reduce the array size toN/2. Add bound check fornew_possible_sum. Throw away greater possible sums.