Very Easy and Begineer-Friendly✨| Constant Time
IntuitionThe problem requires us to design a data structure that can:

Add numbers to a list.


Calculate the product of the last k numbers added.

The challeng
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires us to design a data structure that can:

1. Add numbers to a list.

2. Calculate the product of the last k numbers added.

The challenge is to handle these operations efficiently, especially when zeros are added. If a zero is present in the last k numbers, the product should automatically be 0.

# Approach
<!-- Describe your approach to solving the problem. -->
Here’s how we solve it step by step:

1. Use a List to Track Products:

- We maintain a list (prod) that stores the cumulative product of all numbers added so far.

- For example, if the numbers added are [3, 2, 4], the list will store [1, 3, 6, 24] (starting with 1 to simplify calculations).

2. Handle Zeros:

- When a zero is added, the product resets. So, we push 1 into the list and note the position of this zero.

- For example, if the numbers are [3, 0, 2], the list becomes [1, 3, 1, 2].

3. Calculate the Product of Last k Numbers:

- If k is larger than the number of elements added, return 0.

- If a zero is present in the last k numbers, return 0.

- Otherwise, divide the last cumulative product by the product just before the kth last element to get the result.

# Complexity
- Time complexity: $$O(1)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class ProductOfNumbers {
public:
    vector<long long> prod;
    int lastZero = -1;

    ProductOfNumbers() {
        prod.push_back(1); 
    }

    void add(int num) {
        if (num == 0) {
            prod.push_back(1);
            lastZero = prod.size() - 1; 
        } else {
            prod.push_back(prod.back() * num);
        }
    }

    int getProduct(int k) {
        int sz = prod.size();
        if (k > sz - 1) return 0; 
        if (lastZero >= sz - k) {
            return 0; 
        }
        return prod[sz - 1] / prod[sz - 1 - k];
    }
};

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers* obj = new ProductOfNumbers();
 * obj->add(num);
 * int param_2 = obj->getProduct(k);
 */
```
['Array', 'Prefix Sum', 'C++']