1352.cpp (356B)
1 class ProductOfNumbers { 2 vector<long long> prefix = {1}; 3 4 public: 5 void add(int num) { 6 if (num == 0) 7 prefix = {1}; 8 else 9 prefix.push_back(num * prefix.back()); 10 } 11 12 int getProduct(int k) { 13 if (k >= prefix.size()) return 0; 14 return prefix.back() / prefix[prefix.size() - k - 1]; 15 } 16 };