Interview Question Categories

FIND 3 INTS WHOSE PRODUCT IS THE LARGEST IN THE LIST

FIND 3 INTS WHOSE PRODUCT IS THE LARGEST IN THE LIST


namespace Find3intsWhoseProdIsTheLargestInArray
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> li = new List<int>();

            li.Add(1);
            li.Add(-3);
            li.Add(10);
            li.Add(-10);
            li.Add(20);
            li.Add(5);
            li.Add(4);
            li.Add(8);
            li.Add(-3);
            li.Add(-12);
            
            li.Sort();

            int prod1 = li[0]*li[1]*li[li.Count-1];
            int prod2 = li[li.Count-3]*li[li.Count-1]*li[li.Count-2];

            Console.WriteLine((prod1 > prod2)? prod1: prod2);
        }
    }
}

No comments:

Post a Comment