0

I'm trying to split an array of numbers into two separate arrays that have their sum as close together as possible (I need this for evening out the heights of data in a collection view). My idea is that to do this, I would find all possible arrays I can generate from this initial array. The final arrays would always have an equal length, except for the case where the initial array consists of an odd number of items. If that's the case, one of the final arrays will have one more item.

Sample input: [15,23,13,5,12,18,4,11] Sample output: [12,23,4,11] and [15,18,13,5] (at least I think the sums of 50 and 51 are as close as we're going to get here)

I have tried a lot of code but it's pretty rubbish and is not going to be of help here. With any pointers in terms of which algorithm I'm looking for, I should be able to solve this myself.

I've tried to no avail, and any pointers to algorithms or help as to how to do this are welcome. Thanks!

JillevdW
  • 1,087
  • 1
  • 10
  • 21
  • Show sample input, desired output, and current code attempt. – matt Jul 07 '18 at 07:14
  • @matt I've added a sample input and a desired output. I could add my current code attempt but it is very naively looping through the array, then adding to the output array that currently has the smallest sum. – JillevdW Jul 07 '18 at 07:20
  • Do the two final arrays represent 2 column's items' heights? – Alexander Jul 07 '18 at 07:25
  • @Alexander The final arrays represent the height of each item in the column, so the sum would be the height of the column, correct. – JillevdW Jul 07 '18 at 07:25
  • https://www.geeksforgeeks.org/partition-a-set-into-two-subsets-such-that-the-difference-of-subset-sums-is-minimum/ – Alexander Jul 07 '18 at 07:26
  • @Alexander that's the right direction; however that doesn't take into account that I want final arrays with an equal (or offset 1 from equal) length. – JillevdW Jul 07 '18 at 07:30
  • @JillevdW Are you sure? That would increase the average height difference between the 2 columns – Alexander Jul 07 '18 at 07:40
  • @Alexander since I'm not aware of a way to have objects in a collection view spread out unequally across columns, I don't mind the average height difference. I now know how to calculate it without the constraint of equal array length, so that is cool, but the original question stands :) – JillevdW Jul 07 '18 at 07:44
  • I dupe-marked this as a duplicate of [Divide set of values into two sets of same or similar size with similar value sums](https://stackoverflow.com/questions/32157872/), which covers the same same problem. The answers in the dupe target doesn't really provide a solution to the problem, however (although [this answer](https://stackoverflow.com/a/32158994/4573247) should be of help), so OP: let me know if you want me to undo the dupe mark, in case the target doesn't sufficiently answer you question. Ideally someone should expand with an answer to the dupe target rather than to this thread. – dfrib Jul 07 '18 at 09:24
  • 1
    @dfri thanks for pointing that out, that question put me on the right track. I'm still not sure how to solve the problem, but I found a solution that should come close enough for me. Since this apparently dives into some deeper maths than I thought it would, I'll let it rest. Thanks :) – JillevdW Jul 09 '18 at 06:16

0 Answers0