Simple Prefix Array Count Zero
IntuitionTo efficiently calculate the product of the last k numbers, we need to:
Keep track of the cumulative product as new numbers are added.
Handle the scena
# Intuition
To efficiently calculate the product of the last `k` numbers, we need to:
- Keep track of the cumulative product as new numbers are added.
- Handle the scenario where `0` is present, as it invalidates the product for any range containing it.

# Approach
- Use a vector `numbers` to store cumulative products.
- Maintain a separate vector `zeroIndex` to record indices of zeros.
- When a `0` is added:
  - Reset the cumulative product (`tProduct`) to `1`.
  - Record the index in `zeroIndex`.
- When calculating the product:
  - Check if any zero exists within the range using `hasZero()`.
  - If zero is present, return `0`.
  - Otherwise, divide the cumulative product at the end of the range by the product just before the range starts.

# Complexity
- Time complexity:
  - `add()` is $$O(1)$$.
  - `getProduct()` is $$O(k)$$ in the worst case, when checking for zeros.
  
- Space complexity:
  - $$O(n)$$ for storing cumulative products and zero indices.

# Code
```cpp
class ProductOfNumbers {
    vector<int> zeroIndex;
    vector<long long> numbers;
    long long tProduct = 1;
    int index = 0;

public:
    ProductOfNumbers() {
        numbers.push_back(1); 
    }
    
    void add(int num) {
        if (num == 0) {
            zeroIndex.push_back(index);
            tProduct = 1;
            numbers.push_back(1);
        } else {
            tProduct *= num;
            numbers.push_back(tProduct);
        }
        index++;
    }
    
    bool hasZero(int l, int r) {
        // Check if any zero exists in the range [l, r]
        for (int z : zeroIndex) {
            if (l <= z && z <= r) return true;
        }
        return false;
    }

    int getProduct(int k) {
        int r = numbers.size() - 1;
        int l = r - k;
        
        // Check if zero is in the range
        if (hasZero(l, r)) {
            return 0;
        } else {
            return numbers[r] / numbers[l];
        }
    }
};

['Math', 'Design', 'Data Stream', 'Prefix Sum', 'C++']