Problems › Arrays & strings

Product of Array Except Self

Difficulty: Medium · Topic: Arrays & strings

Given an array nums, return an array result where result[i] is the product of all elements except nums[i]. Solve it in O(n) without using division (so a zero in the input doesn't break you).

Examples

product_except_self([1,2,3,4])
→ [24,12,8,6]

24=2·3·4, 12=1·3·4, …

product_except_self([2,3])
→ [3,2]

Target complexity: time O(n), space O(1)

Write your solution in Python and run it against hidden tests right on this page. Progressive hints and Python + C++ reference solutions are included.

Learn the idea first: the lesson for this problem.

More Arrays & strings problems