I ve been searching for various algorithms for the past few days and I came accross this one.
Is this the right solution to it (Greedy algorithm), or am i missing something.

 static void Main(string[] args)
    {

        int[] array = new int[71]{
        72, 79, 84, 46, 14, 4, 62, 88, 58, 21, 16, 27, 13, 52, 96, 70, 28, 75,
        76, 45, 5, 71, 43, 87, 64, 95, 72, 22, 43, 15, 25, 70, 64, 93, 56, 90,
        84, 31, 49, 71, 29, 46, 6, 17, 81, 21, 75, 44, 2, 45, 67, 57, 93, 72,
        39, 74, 43, 53, 40, 45, 87, 23, 44, 52, 1, 56, 32, 26, 32, 67, 41};
        Array.Sort(array);
        List<int> lista = array.ToList();
        List<int> A = new List<int>();
        List<int> B = new List<int>();
        A.Add(lista[0]);
        B.Add(lista[1]);
        for (int i = 0; i <lista.Count; i++)
        {
            if (A.Sum() > B.Sum())
            {
                B.Add(i);
            }
            else 
            {
                A.Add(i);
            }
        }
        Console.WriteLine(A.Sum());
        Console.WriteLine(B.Sum());
        Console.ReadLine();
    }

Recommended Answers

All 4 Replies

Wouldn't it be more accurate if you sort descending (or loop in reverse order), so the largest items come first?

Remove lines 13 and 14, they are adding duplicates.

I m sorry, my for loop had to start at i=2;
-Would'nt the output be the same if i reverse the list and start from top element?

Would'nt the output be the same if i reverse the list and start from top element?

Depends on the numbers. Imagine a list of a hundred 1's and one 100.

Yeap, I don't know how i missed that.
If starting from the top to bottom wouldn't then this be the most accurate way to rephrase your sentence, or is there a more accurate way since all the numbers are sorted. Am i missing something?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.