# Product of Array Except Self leetcode.com/problems/product-of-array-except-self/ Brute force: calculate product of all elements, count the number of `0`s. ```python product = 1 zeros = 0 for num in nums: if num: product *= num else: zeros += 1 if not zeros: return [product // num for num in nums] elif zeros == 1: return [0 if num else product for num in nums] else: return [0] * len(nums) ``` Alternative: calculate prefix and suffix products. ```python n = len(nums) pl = [1] * n pr = [1] * n for i in range(1, n): pl[i] = pl[i - 1] * nums[i - 1] for i in range(n - 2, -1, -1): pr[i] = pr[i + 1] * nums[i + 1] return [pl[i] * pr[i] for i in range(n)] ``` This method is an example of [[dp]].