O(1) GetProduct | Efficient Prefix Product with Instant Reset on Zero 🔥Beats 99.1
IntuitionUse a prefix product list (ll) to store cumulative products.
Reset (ll = [1]) if 0 is added, since any product including 0 is 0.
Fetch product in 𝑂(1)
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Use a prefix product list (ll) to store cumulative products.
Reset (ll = [1]) if 0 is added, since any product including 0 is 0.
Fetch product in 𝑂(1) using division:
![image.png](https://assets.leetcode.com/users/images/3cb32425-7377-47f1-a804-2031965cebf0_1739506601.1079805.png)

# Approach
![image.png](https://assets.leetcode.com/users/images/c21979d1-c73d-4227-86f9-cedd89a8c03a_1739506659.53355.png)


# Complexity
![image.png](https://assets.leetcode.com/users/images/4c7bba89-b00b-4beb-96c4-e0bd8caabf5c_1739506687.363507.png)

![image.png](https://assets.leetcode.com/users/images/85383cdc-a362-4a03-8846-06f3d5711e37_1739506830.9343095.png)



# Code
```java []
class ProductOfNumbers {
    List<Integer> ll;
    public ProductOfNumbers() {
        ll = new ArrayList();
        ll.add(1);
    }
    
    public void add(int num) {
        if(num==0){
            ll = new ArrayList();
            ll.add(1);
        }
        else ll.add(ll.get(ll.size()-1)*num);
    }
    
    public int getProduct(int k) {
        if(k>ll.size()-1) return 0;
        else return ll.get(ll.size()-1)/ll.get(ll.size()-1-k);
    }
}
```
![ok.png](https://media.pinatafarm.com/protected/B183D0EF-49B8-47BF-A523-E72FD0CFFAAC/sad-thumbs-up-cat.3.meme.webp)
['Java']