Loading...
Given an integer array nums, return an array result of the same length where result[i] is the product of every element of nums except nums[i].
You can assume the product of any prefix or suffix of nums fits in a 32-bit signed integer.
You must write an algorithm that runs in O(n) time without using the division operation.
Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Explanation: result[0] = 2*3*4 = 24, result[1] = 1*3*4 = 12, result[2] = 1*2*4 = 8, result[3] = 1*2*3 = 6.
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]
Explanation: Every position except index 2 has a zero among the other elements, so those products are 0; result[2] = (-1)*1*(-3)*3 = 9.
nums.length ≤105nums[i] ≤30nums (and every entry of the answer) fits in a 32-bit signed integer.Click "Run" to test with sample cases or "Submit" to run all tests.