- [Link to problem](https://leetcode.com/problems/product-of-array-except-self/)
- [Solution with explanation](https://leetcode.com/problems/product-of-array-except-self/discuss/1597994/C%2B%2BPython-4-Simple-Solutions-w-Explanation-or-Prefix-and-Suffix-product-O(1)-space-approach)
---
### Problem
Given an integer array `nums`, return _an array_`answer`_such that_`answer[i]`_is equal to the product of all the elements of_`nums`_except_`nums[i]`.
The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
You must write an algorithm that runs in`O(n)`time and without using the division operation.
#### Examples
**Example 1:**
**Input:** nums = [1,2,3,4]
**Output:** [24,12,8,6]
**Example 2:**
**Input:** nums = [-1,1,0,-3,3]
**Output:** [0,0,9,0,0]
#### Constraints
-`2 <= nums.length <= 105`
-`-30 <= nums[i] <= 30`
- The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
### Thoughts
> [!summary]
> This is a variation of #prefix_sum
I thought of using prefix product, a variation of
prefix_sum, which perfectly fits the requirement.
#### First iteration:
keep two arrays: the suffix product and the prefix product
of the `nums` array.
Then, `ans = prefix[i] * suffix[i]`
#### Second iteration:
> It's like rolling snowball, the suffix sum variable
> gets reused
We can use only one output array:
write the prefix to the `ans` array, and update it from
end using suffix sum variable.
This achieves the `O(1) space except output` requirement.
#### Third iteration:
Since the multiplying process is independent, and can be
reversed, We can do that in one loop.
`1 * A * B == 1 * B * A`
If we initialize the ans array as one, we can do that in one