<p>Given an integer array <code>nums</code>, return <em>an array</em> <code>answer</code> <em>such that</em> <code>answer[i]</code> <em>is equal to the product of all the elements of</em> <code>nums</code> <em>except</em> <code>nums[i]</code>.</p>

<p>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</p>

<p>You must write an algorithm that runs in&nbsp;<code>O(n)</code>&nbsp;time and without using the division operation.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> [24,12,8,6]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> nums = [-1,1,0,-3,3]
<strong>Output:</strong> [0,0,9,0,0]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-30 &lt;= nums[i] &lt;= 30</code></li>
	<li>The product of any prefix or suffix of <code>nums</code> is <strong>guaranteed</strong> to fit in a <strong>32-bit</strong> integer.</li>
</ul>

<p>&nbsp;</p>
<p><strong>Follow up:</strong>&nbsp;Can you solve the problem in <code>O(1)</code>&nbsp;extra&nbsp;space complexity? (The output array <strong>does not</strong> count as extra space for space complexity analysis.)</p>

class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        // Initialize a vector to store the results and a variable to keep track of the product of elements to the right
        int product = 1;
        vector<int> output(nums.size());  // Create an output vector of the same size as input

        // Step 1: Compute the prefix products (product of all elements to the left of the current element)
        for (int i = 0; i < nums.size(); i++) {
            if (i == 0) {
                // For the first element, there's no element to the left, so the prefix product is simply the element itself
                output[i] = nums[i];
            } else {
                // For other elements, the prefix product is the product of the previous prefix product and the current element
                output[i] = output[i - 1] * nums[i];
            }
        }

        // Step 2: Compute the suffix products (product of all elements to the right of the current element) and update the output array
        for (int i = nums.size() - 1; i >= 0; i--) {
            // If we are at the last element, there's no element to the right, so we use the prefix product directly
            if (i == nums.size() - 1) {
                output[i] = output[i - 1];  // For the last element, the product is simply the prefix product of the second last element
            } else if (i == 0) {
                // For the first element, we only have the suffix product
                output[i] = product;  // Product variable here stores the suffix product for the first element
            } else {
                // For other elements, multiply the current prefix product by the suffix product
                output[i] = product * output[i - 1];
            }
            
            // Update the product variable to include the current element (moving right)
            product *= nums[i];
        }

        return output;  // Return the result vector
    }
};

38400