Project Euler #8: Largest product in a series

  • + 0 comments

    If you have test cases from 5 to 9 (both inclusively) failing, it means that is: (i) a memory scaling issue (where the first comment of this thread applies), (ii) probably performing unnecessary computations, (iii) missing a necesary computation.

    Two things helped me for these, besides treating each digit individually to solve (i): (a) Use a sliding window approach. Compute the first window (0 to k - 1) once and move the window progressively. This is faster O(n) than the naïve approach O(n*k). (b) Track the amount of zeros with a counter. Update the zero count if a zero leaves or enters the window and act accordingly with the product. This should solve all the remaining test cases (which are for large k and n)