Using the Greedy Algorithm One approach to the problem that mimics how children select teams to play with is a greedy algorithm that iterates over numbers in descending order, assigning each of them a smaller subset. This works well when the numbers in a set are about the same size as its power or less.
Try this in python: If an average exists, then this one can find it, otherwise it will bring you closer to an equal average.
a = [500, 1, -45, 46, -20, 100, -30, 4, 110] b, c = [], [] m1, m2, n1, n2 = 0, 0, 0, 0 count = 0 for i in a: if count == 0: b.append(i) n1 += 1 m1 = i n1 = 1 count = 1 continue else: tmp_m1 = (m1 * n1 + i) / (n1 + 1) tmp_m2 = (m2 * n2 + i) / (n2 + 1) print b, c, tmp_m1, tmp_m2 if m1 > m2: if(tmp_m1 - m2 < m1 - tmp_m2): b.append(i) m1 = tmp_m1 n1 += 1 else: c.append(i) m2 = tmp_m2 n2 += 1 else: if(tmp_m1 - m2 < m1 - tmp_m2): c.append(i) m2 = tmp_m2 n2 += 1 else: c.append(i) m1 = tmp_m1 n1 += 1 print b, c, m1, m2 Sample output: [500] [] 250 1 [500, 1] [] 151 -45 [500, 1, -45] [] 124 46 [500, 1, -45] [46] 108 13 [500, 1, -45, -20] [46] 106 73 [500, 1, -45, -20] [46, 100] 80 38 [500, 1, -45, -20, -30] [46, 100] 67 50 [500, 1, -45, -20, -30, 4] [46, 100] 73 85 [500, 1, -45, -20, -30, 4] [46, 100, 110] 73 73