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.
Sliding window problem. You can bail on computing sub-products whenever you encounter a zero (in my case, that's when tmp becomes zero).
int max_prod(string num, size_t n, int k) {
int res = 0;
size_t l = 0;
size_t r;
while (l < (n - k)) {
r = l;
int tmp = 1;
while (tmp > 0 && r < l + k) {
tmp *= (num[r] - '0');
if ((r - l == (k - 1)) && tmp > res) {
res = tmp;
}
r++;
}
l++;
}
return res;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #8: Largest product in a series
You are viewing a single comment's thread. Return to all comments →
Sliding window problem. You can bail on computing sub-products whenever you encounter a zero (in my case, that's when tmp becomes zero). int max_prod(string num, size_t n, int k) { int res = 0; size_t l = 0; size_t r;
}