Product of Array Except Self
Given an integer array nums, return an array answer where answer[i] is the product of all elements of nums except nums[i]. You must solve it without using the division operator and in O(n) time.
Open official problem prompt ↗Produce, for each position, the product of the whole array with that one element left out — cheaply and without dividing.
Two people count a line of runners: one walks left to right recording how many are ahead of each runner, the other walks right to left recording how many are behind. Multiply the two tallies at each runner to know everyone but themselves.
- Input
- nums = [1, 2, 3, 4]
- Output
- [24, 12, 8, 6]
- Why
- answer[0]=2*3*4=24, answer[1]=1*3*4=12, answer[2]=1*2*4=8, answer[3]=1*2*3=6.
2 <= nums.length <= 10^5-30 <= nums[i] <= 30The product of any prefix or suffix of nums fits in a 32-bit integerDivision operator is not allowed