0

I want the combination that gets me closest to 1000 by adding the numbers together.

those are my numbers {500,498,4,900, 4}

like this: 500 + 498 = 998 which is just 2 away from 1000 and like this 500 + 498 + 4 = 1002 which is also 2 away.

i'm trying to do something like this

List<int> list = new List<int> { 4, 900, 500, 498, 4 };
        int number = 1000;
        int closest = list.OrderBy(item => Math.Abs(number - item)).First();
        Console.WriteLine(closest);
        Console.ReadLine();

but I think I doing it in a wrong way!

what you suggest? How do I solve it.

Melad Batta
  • 216
  • 1
  • 5
  • 14
  • ...not very helpful, I suggest you add a little more detail on what's happening. – Kyle Needham Mar 03 '15 at 22:43
  • I get this messege "Unable to retrieve property split for a reference that is undefined or null" – Melad Batta Mar 04 '15 at 08:32
  • Possible duplicate of [finding all possible combinations of numbers to reach a given sum](http://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum). – Georg Patscheider Mar 16 '16 at 07:56

1 Answers1

1

You have an error in your for loop. There is an extra semicolon.

you have:

 for (var i = 0; i < splitnamn.length - 1; i++); {

should be:

 for (var i = 0; i < splitnamn.length - 1; i++) {
Leo Farmer
  • 7,730
  • 5
  • 31
  • 47