> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Product of Array Except Self Python Solution

> Tested Python solution for LeetCode 238 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 238, Medium. Topics: Array, Prefix Sum. [View on LeetCode](https://leetcode.com/problems/product-of-array-except-self/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 238   # by problem number
lcpy gen -s product_of_array_except_self   # by problem name
```

## 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

```
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
```

```
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
```

### Constraints

* 2 \<= nums.length \<= 10^5
* -30 \<= nums\[i] \<= 30
* The input is generated such that answer\[i] 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.)

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/product_of_array_except_self/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/product_of_array_except_self/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def product_except_self(self, nums: list[int]) -> list[int]:
        # Example: nums = [1, 2, 3, 4]
        # Expected output: [24, 12, 8, 6]

        n = len(nums)
        result = [1] * n  # [1, 1, 1, 1]

        # Left pass: result[i] = product of all elements to the left of i
        # nums:   [1, 2, 3, 4]
        # result: [1, 1, 2, 6] (left products)
        for i in range(1, n):
            result[i] = result[i - 1] * nums[i - 1]

        # Right pass: multiply by product of all elements to the right of i
        # right products: [24, 12, 4, 1]
        # result: [1*24, 1*12, 2*4, 6*1] = [24, 12, 8, 6]
        right = 1
        for i in range(n - 1, -1, -1):
            result[i] *= right
            right *= nums[i]

        return result
```

## Complexity

| Time | Space |
| ---- | ----- |
| O(n) | O(1)  |

## Tags

[Grind 75](/catalog/grind-75), [Grind](/catalog/grind), [Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode), [AlgoMaster 75](/catalog/algo-master-75).
