Product of Array Except Self

LeetCode problem 238. Product of Array Except Self.

## Problem description

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.

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.

Follow up
Can you solve the problem in `O(1)` extra space complexity? (The output array **does not** count as extra space for space complexity analysis.)
## Analysis

The analysis will directly address the approach to solve the problem in O(1) extra space complexity.

Since we are not allowed to use the division operation, the idea of computing *cumulative products* comes to mind. To make use of this concept, we should notice that each results consist of *left products* (product of numbers to the left of the current number) and *right products* (product of numbers to the right of the current number). Here is an example to illustrate this:

* Inputs: [3, 4, 5, 6, 7]

* Red color indicate numbers appended to left products at each position.
* Blue color indicate numbers appended to right products at each position.

So we can solve this problem with the following steps:

1. Initialize the `results` array, whose length is the same as the input `nums`.
2. Traverse the `nums` from left to right, and put the left cumulative products in the `results` array.
3. Traverse the `nums` from right to left, and update the `results` array using the right cumulative products.
	* Use a `temp` variable to keep track of the right product during each iteration.
	* After computing the right product at position *i*, multiply `results[i]` by `temp` to update `results[i]`.
4. Return `results`.

### Complexity analysis

Time complexity
Since we traversed through the input `nums` once from left to right and once from right to left, and each iteration has constant computation, the total time complexity is **O(n)**.
Space complexity
Since we only have used the `temp` variable to store the right cumulative product, which is updated during each iteration in the right-to-left phase, the extra space complexity is **O(1)**.
## Solution

### C++ solution

```
class Solution {
public:
 std::vector<int> productExceptSelf(std::vector<int>& nums) {
 const int len = nums.size();
 std::vector<int> results(len);

 // step 1: traverse from left to right
 for (int i = 0; i < len; ++i) {
 results[i] = (i == 0) ? 1 : (results[i-1] \* nums[i-1]);
 }
 // step 2: traverse from right to left
 int temp = 1;
 for (int i = len - 1; i >= 0; --i) {
 temp = (i == len - 1) ? 1 : (temp \* nums[i+1]);
 results[i] \*= temp;
 }

 return results;
 }
};

```

### Java solution

```
class Solution {
 public int[] productExceptSelf(int[] nums) {
 final int len = nums.length;
 int results[] = new int[len];

 // step 1: traverse from left to right
 for (int i = 0; i < len; ++i) {
 results[i] = (i == 0) ? 1 : (results[i-1] \* nums[i-1]);
 }
 // step 2: traverse from right to left
 int temp = 1;
 for (int i = len - 1; i >= 0; --i) {
 temp = (i == len - 1) ? 1 : (temp \* nums[i+1]);
 results[i] \*= temp;
 }

 return results;
 }
}

```