✅ 🌟 JAVA SOLUTION ||🔥 BEATS 100% PROOF🔥|| 💡 CONCISE CODE ✅ || 🧑‍💻 BEGINNER FRIENDLY
Complexity
Time complexity:O(1)

Space complexity:O(N)
Code
# 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
```java []
class ProductOfNumbers {
    ArrayList<Integer> ans=new ArrayList<>();
    int prod=1;
    public ProductOfNumbers() {
    }
    
    public void add(int num) {
        if(num==0)
        {
            ans=new ArrayList<>();
            prod=1;
            return;
        }
        prod*=num;
        ans.add(prod);
    }
    
    public int getProduct(int k) {
        if(ans.size()<k) return 0;
        else if(ans.size()==k) return prod;
        return prod/ans.get(ans.size()-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', 'Math', 'Design', 'Data Stream', 'Prefix Sum', 'Java']