Build the output using prefix and suffix products in two passes without division
For each index i, the answer is product(nums[0..i-1]) × product(nums[i+1..n-1]). Computing each from scratch costs O(n) per index, totaling O(n²). The prefix/suffix decomposition precomputes running products so each element's answer is assembled in O(1) from previously computed partial products.
Prefix and suffix products are independent — the prefix at index i depends only on elements before i, and the suffix only on elements after i. This separability enables the two-pass linear solution.
out: O(n) but excluded per problem definitionsuffix: single scalar variable for running suffix producti: single integerA naive approach uses two separate O(n) arrays for prefix and suffix products. This optimization reuses the output array itself as the prefix array, then multiplies suffix products directly into it with only one extra variable. The output array serves dual purpose: storage for the answer and workspace for intermediate computation.
The output array is cleverly repurposed as scratch space: first it stores prefix products, then each entry is multiplied in-place by the running suffix. This eliminates the need for a separate suffix array.