loading...

re: Daily Coding Problem #2 VIEW POST

FULL DISCUSSION
 

O(n) with division, in C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace Daily_Coding_Problem_2
{
    public class Program
    {
        static void Main(string[] args)
        {
            var numbers = GetNumberList();

            var ret = Problem2(numbers);

            foreach (var i in ret)
            {
                Console.WriteLine(i);
            }
        }

        public static IEnumerable<int> GetNumberList()
        {
            return Console.ReadLine() ?
                              .Split(' ')
                              .Select(int.Parse)
                              .ToArray();
        }

        public static IEnumerable<int> Problem2(IEnumerable<int> numbers)
        {
            var enumerable = numbers as int[] ?? numbers.ToArray();

            var product = enumerable.Aggregate(1, (current, n) => current * n);

            return enumerable.Select(n => product / n).ToList();
        }
    }
}
Code of Conduct Report abuse