15% off all plans & roadmaps Use code
See plansLeetCode 238 · Solution & editorial
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].
Product of Array Except Self is a Array/Matrix Manipulation Patterns problem, in the Product Except Self (Prefix/Suffix Products) subpattern. The pattern covers in-place operations, hashing, prefix sums, spiral traversal, and cyclic sort
Master the prefix and suffix products technique to calculate product of array except self without division.
The reference solution runs in O(n) time and O(1) space.
Product of Array Except Self has been reported in interviews at Facebook, Amazon, Apple, Asana, Lyft, Microsoft, Uber, Adobe.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Product of Array Except Self is a Array/Matrix Manipulation Patterns problem, in the Product Except Self (Prefix/Suffix Products) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.