C++ Prefix Array
We maintain the runnig product in the prefix array. To get the product for the last k elements, we divide the last running product by the product that was k - 1
We maintain the runnig product in the prefix array. To get the product for the last `k` elements, we divide the last running product by the product that was `k - 1` steps ago. \n\nImportant observation: if we add zero, we reset the prefix array. All products that includes zero will be zero. So, we can just check if `k` is greater than the size of our previx array, and return zero in that case.\n\n```CPP\nvector<long long> pr = { 1 };\nvoid add(int num) {\n    if (num == 0) pr = { 1 };\n    else pr.push_back(pr.back() * num);\n}\nint getProduct(int k) {\n    if (k >= pr.size()) return 0;\n    return pr.back() / pr[pr.size() - k - 1];\n}\n```
[]