Skip to main content

Product of Array Except Self

MediumPremium

Given an array nums of n integers where n > 1, return an array output such that each element at index i of output is equal to the product of all the numbers in the original array except nums[i].

Examples

nums = [1, 2, 3, 4] output: [24, 12, 8, 6] nums = [0, 0] output: [0, 0] nums = [4, 5, 1, 8, 2] output: [80, 64, 320, 40, 160] nums = [-2, 1, -3, 4, -1] output: [12, -24, 8, -6, 24]

Can you design an algorithm with a time complexity of O(n)?