-3

I got 9 numbers which I want to divide in two lists, and both lists need to reach a certain amount when summed up. For example I got a list of ints:

List<int> test = new List<int>
{
    1963000, 1963000, 393000, 86000,
    393000, 393000, 176000, 420000,
    3193000
};

And I want to have 2 lists of numbers that when you sum them up, they both reach over 4 million.

It doesn't matter if the 2 lists don't have the same amount of numbers. If it only takes 2 numbers to reach 4 million in 1 list, and 7 numbers together reaching 7 million, is fine.

As long as both lists summed up are equal to 4 million or higher.

Lasse V. Karlsen
  • 380,855
  • 102
  • 628
  • 825
Quoter
  • 4,236
  • 13
  • 47
  • 69
  • 3
    Hey, you should know the routine already, what have you tried, and where are you stuck? What is stopping you from implementing it yourself? – Icepickle Dec 08 '18 at 22:54
  • @Icepickle, I have been trying to google this up. Someone must have tried this before. But at no avail. No idea how to start or set it up either. – Quoter Dec 08 '18 at 23:06
  • Is this a homework question? – John Dec 08 '18 at 23:46
  • @John, no this is for a strategy game. A hobby project. – Quoter Dec 08 '18 at 23:48
  • Not sure what you are asking – Aluan Haddad Dec 09 '18 at 02:29
  • @AluanHaddad, split a list of numbers into two new lists. And when both lists of numbers are summed up separately, they both should be 4 million or higher. And the amount of numbers in each list doesn't matter. As long as both lists have 4 million or higher when summed up. – Quoter Dec 09 '18 at 14:38

1 Answers1

1

Is this certain sum low enough to be reached easily? If yes, then your algorithm may be as simple as: iterate i from 1 to number of items. sum up the first i numbers. if the sum is higher than your certain sum (eg 4 million), then you are finished, else increment i.

BUT: if your certain sums are high and it is not such trivial to find the partition, then you have the famous Partition Probem (https://en.wikipedia.org/wiki/Partition_problem), this is not that simple but there are some algorithms. Read this wikipedia artikle or try to google "Partition problem solution" or similar.

gofal3
  • 1,213
  • 10
  • 16
  • I can't know upfront whether its reached easily. But all combinations have to be tried. – Quoter Dec 08 '18 at 23:50
  • 1
    if you have to try all combinations, that means for each of the 9 items you have to try if it is in the first or the second sum. So 2 possible states for 9 items, 2^9 = 512 calcualtions to try. That is possible for a nice computer, as long as the number if items will not grow :-) – gofal3 Dec 08 '18 at 23:57
  • 9 numbers is the max yes. So how can I divide that into 2 lists with numbers who's sum is >= 4M? Won't that just give me 1 list? – Quoter Dec 09 '18 at 00:02
  • 1
    you know about binary numbers? if you take any number X from 0 to 2^9-1 = 511, then you can decide if the I th number is in the first set if the I th bit of X is set to 1, so if (X & (2^I) > 0) – gofal3 Dec 09 '18 at 00:07
  • - there are 10 kinds of people: those who understand binary and those who don't - :-))) I love this joke so much! – gofal3 Dec 09 '18 at 00:16
  • But the list hasn't been split up yet, so how can I tell which number is in the first set or not? – Quoter Dec 09 '18 at 10:23
  • 1
    0 is binary 000000000b, so each of the 9 bits are 0 so all of them are in one set; 1 is binary 000000001b, so the first bit is 1 (the first number is in one list) and the other 8 are 0 (they are in the other list); 2 is binary 000000010b, so the second is in the one list and all other in the other list 3 is binary 000000011b, so ehe first and the second both are in the one list and the rest in the other list 4 is binary 000000100b, so the third item is in the one list and the rest in the other list 5 is binary 000000101b, so the third and the first item are in the one list and .... – gofal3 Dec 09 '18 at 11:22
  • I'm sorry, it's not very readable in a comment. Also, not sure where the split needs to happen. – Quoter Dec 09 '18 at 12:41