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...
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;
}
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...
int max_prod(string num, size_t n, int k) { int res = 0; size_t l = 0; size_t r;
}