Melad Batta
Melad Batta

Reputation: 216

algorithm combination that gets us closest to 1000

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.

Upvotes: 0

Views: 232

Answers (1)

Leo Farmer
Leo Farmer

Reputation: 7910

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++) {

Upvotes: 1

Related Questions