Project Euler #8: Largest product in a series

  • + 0 comments

    Okay. I'm improving. This should have taken me atleast an hour. Done it in 20 mins. C# solution

    private static int getProduct(int[] arr)
        {
            int product = 1;
            foreach(int x in arr)
                product *= x;
            return product;
        }
        
        public static void printAnswer(string num, int n, int k)
        {
            int highestProduct = 0;
            int c = n-k+1; //max combinations
            for(int i = 0; i < c; i++)
            {
                string newStr = num.Substring(i,k);
                int [] arr = newStr.Select(x => x - '0').ToArray();
                int product = getProduct(arr);
                if(product > highestProduct)
                {
                    highestProduct = product;
                }
            }
            Console.WriteLine(highestProduct);
        }