Beats 99.69% | Efficient Data Structure for Computing Product of Last K Numbers
ProductOfNumbers ImplementationIntuitionThe problem requires maintaining a data structure that efficiently computes the product of the last k numbers. The main
## ProductOfNumbers Implementation

### Intuition
The problem requires maintaining a data structure that efficiently computes the product of the last `k` numbers. The main challenge is handling zeros, as they reset the product. Using a cumulative product list helps retrieve results in constant time.

![image.png](https://assets.leetcode.com/users/images/082f2953-30f6-4c0c-b5ff-08555ff118d3_1739527504.8795505.png)

### Approach
1. **Initialization:** Maintain a list storing cumulative products.
2. **Add Operation:** Multiply the new number with the last stored cumulative product.
   - If the number is zero, reset the list because zero invalidates all previous products.
3. **Get Product:** Retrieve the product of the last `k` numbers by dividing the last cumulative product by the cumulative product `k` steps back.
   - If `k` exceeds the number of stored elements, return `0`.

### Complexity
- **Time Complexity:**
  - `add(num)`: $$O(1)$$
  - `getProduct(k)`: $$O(1)$$
- **Space Complexity:** $$O(n)$$

### LeetCode Solution & Test Cases

| Test Case | Operations | Expected Output | Explanation |
|-----------|------------|----------------|-------------|
| 1 | `add(3)`, `add(4)`, `add(2)`, `getProduct(2)` | `8` | The last two numbers are `[4, 2]`, so `4 * 2 = 8` |
| 2 | `add(1)`, `add(2)`, `add(3)`, `add(0)`, `add(5)`, `getProduct(2)` | `5` | The zero resets the product list, so the last two numbers are `[0, 5]`, and their product is `5` |

### Pseudocode Explanation
#### Test Case 1:
```
Initialize an empty list.
add(3) → list = [3]
add(4) → list = [3, 12]
add(2) → list = [3, 12, 24]
getProduct(2): return list[2] / list[0] = 24 / 3 = 8
```
#### Test Case 2:
```
Initialize an empty list.
add(1) → list = [1]
add(2) → list = [1, 2]
add(3) → list = [1, 2, 6]
add(0) → list reset to []
add(5) → list = [5]
getProduct(2): Not enough elements, return 5
```

### Code
```java
import java.util.ArrayList;

class ProductOfNumbers {
    ArrayList<Integer> li = new ArrayList<>();
    int pro = 1;

    public ProductOfNumbers() {
    }

    /**
     * Adds a number to the data structure. If the number is zero, the list is reset
     * since zero invalidates all previous products.
     */
    public void add(int num) {
        if (num == 0) {
            li = new ArrayList<>();
            pro = 1;
            return;
        }
        pro *= num;
        li.add(pro);
    }

    /**
     * Returns the product of the last k numbers.
     * If k is greater than the number of elements stored, it returns 0.
     */
    public int getProduct(int k) {
        if (li.size() < k)
            return 0;
        int ans = li.get(li.size() - 1);
        if (li.size() == k)
            return ans;
        return ans / li.get(li.size() - 1 - k);
    }
}
```

['Array', 'Math', 'Prefix Sum', 'Java']