💡♨️JAVA Solution💡O(1) Approach‼️ Beats 99% ‼️Using Array List💻🚀
IntuitionThe problem requires designing a data structure that allows:
Efficiently appending numbers to a stream.
Quickly retrieving the product of the last k nu
# Intuition
The problem requires designing a data structure that allows:
- Efficiently **appending** numbers to a stream.
- Quickly retrieving the **product of the last `k` numbers**.

A **brute force** approach would involve iterating over the last `k` elements every time `getProduct(k)` is called, resulting in **O(k) time complexity**.  
Instead, we can optimize this using **prefix products**, reducing `getProduct(k)` to **O(1)**.

---


# Complexity
- **Time complexity:**
  - `add(num)`: **O(1)** (appending to the list)
  - `getProduct(k)`: **O(1)** (single division operation)

- **Space complexity:** **O(n)** (storing prefix products)

---
# Code
```java []
class ProductOfNumbers {

    ArrayList<Integer> numbers;
        public ProductOfNumbers() {
            numbers = new ArrayList<>();
        }
        public void add(int num) {
            if(num==0) {
                numbers.clear();
                return;
            }
            if(numbers.isEmpty())
                numbers.add(num);
            else
                numbers.add(num * numbers.get(numbers.size() - 1));
        }
        public int getProduct(int k) {
            if(k>numbers.size())
                return 0;
            if(k==numbers.size())
                return numbers.getLast();
            int product = numbers.getLast()/numbers.get(numbers.size() - k-1);
            return product;
        }
}

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers obj = new ProductOfNumbers();
 * obj.add(num);
 * int param_2 = obj.getProduct(k);
 */
```
['Array', 'Math', 'Design', 'Data Stream', 'Prefix Sum', 'Java']