Kotlin. Beats 100% (62 ms). Static IntArray cache.
CodeApproachIf all the numbers in the product were greater than 0, then we would cache all the products of the added numbers and then simply return the differen
![image.png](https://assets.leetcode.com/users/images/cb9b522e-dca4-4538-876f-c76e3b2a723a_1739544056.012229.png)

# Code
```kotlin []
class ProductOfNumbers() {

    var size = 1
    var zeroArea = 0

    fun add(num: Int) {
        cache[size] = when {
            num == 0 -> { zeroArea = size; 1 }
            else -> cache[size - 1] * num
        }
        size++
    }

    fun getProduct(k: Int): Int = when {
        size > zeroArea + k -> cache[size - 1] / cache[size - 1 - k]
        else -> 0
    }

    companion object {
        val cache = IntArray(30_001)
        init {
            cache[0] = 1
        }
    }
}
```

# Approach

If all the numbers in the product were greater than 0, then we would cache all the products of the added numbers and then simply return the difference in the product between (size - 1) and (size - k - 1)

"0" spoils the whole picture, because all prefixes in the product to the left of "0" automatically become 0.

There are two options here - "zero" the prefixes that are to the left, or reset the cache and start counting again after all the numbers that are to the right of 0.

I prefer the option of "zeroing" the area to the left of 0. However, we don't need to clear the values ​​directly in the cache - instead, we can track the position of the last "0" and, if the left boundary of our product range intersects with the "zeroArea" area, we assume that the product will be equal to 0.
['Kotlin']