We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Project Euler #8: Largest product in a series
You are viewing a single comment's thread. Return to all 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 approachO(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)