Split a set of numbers into 2 sets, where the sum of each set is as close to one another as possible Given a finite set $P = \{ p_1, \dots, p_n \} $ of integers, I'd like to split it into two subsets $A = \{ a_1, \dots, a_m \} \subseteq P$ and $B = \{ b_1, \dots, b_r \} \subseteq P$, where $m + r = n$, and, for each subset, the sum of the numbers is as close as possible to half the total of the sum of the numbers in $P$. More precisely, we have the following constraints
$$\sum_i^m a_i \leq \left \lceil \frac{\sum_j^n p_j}{2} \right \rceil $$
and
$$\sum_i^r b_i \leq \left \lceil \frac{\sum_j^n p_j}{2} \right \rceil $$
and 
$$\sum_i^m a_i + \sum_i^r b_i \leq \sum_j^n p_j $$

How would I go about doing this in a programmatic way?
http://en.wikipedia.org/wiki/Partition_problem
Sort your set DESC, like if you've [1,3,2,5,6], after DESC sort it will look like this: [6,5,3,2,1].
Now you'll have 'SET1' and 'SET2' as your sub sets.
if (SET1&lt;=SET2){The element in the given set should come into the 'SET1';}
else{The element in the given set should come into the 'SET2';}
And of-course you'll eliminate the element you enter into the subsets 'SET1' OR 'SET2' from the given super set.