238. Product of Array Except Self
Last updated
Last updated
See https://leetcode.com/problems/product-of-array-except-self/
Note that the solution needs to run in O(N) time and division can not be used.
Given an input array of nums =[2,3,4,5] the output array is calculated as follows:
index 0 = nums[1] * nums[2] * nums[3] (3 * 4 * 5 = 60)
index 1 = nums[0] * nums[2] * nums[3] (2 * 4 * 5 = 40)
index 2 = nums[0] * nums[1] * nums[3] (2 * 3 * 5 = 30)
index 3 = nums[0] * nums[1] * nums[2] (2 * 3 * 4 = 24)
Result is [60, 40, 30, 24]
Since the solution needs to run in O(N) time we cannot use nested loops. However, we can use multiple loops sequentially as that is a O(N) runtime. For example, two sequential loops would be O(N) + O(N) which reduces to O(N).
Looking at how each index is calculated, the pattern is to multiply the numbers on the left side of the current index with the numbers on the right side of the index. In the diagram below the greyed out cell is the current element.
nums[0] = (no left side) (3 * 4 * 5)
nums[1] = (2) * (4 * 5)
nums[2] = (2 * 3) * (5)
nums[3] = (2 * 3 * 4) (no right side)
And a longer example where nums = [10, 20, 30, 40, 50, 60, 70, 80, 90]
nums[4] = (10 * 20 * 30 * 40) * (60 * 70 * 80 * 90)
This logic can be broken into two loops. The first loop will multiply the numbers to the left of the current index. These values are stored in a "left" array.
Note that the value of left[i] is the cumulative product. For example:
Using nums = [10, 20, 30, 40, 50, 60, 70, 80, 90] as an example:
left[0] = 1
left[1] = 10 * 1
left[2] = 20 * (10 * 1)
left[3] = 30 * ( 20 * (10 * 1))
left[4] = 40 * (30 * ( 20 * (10 * 1)))
and so on...
Note that the first element does not have a left side to multiply. The first element only has right side multiplication. So, in the left array, the first element will default to 1. A similar situation happens for the last element, except it has no right side to multiply. So the last element in the right array will default to 1.
The second loop will multiply numbers to the right of the current index. The for loop starts at the 2nd to last element, because the last element will be 1 for the reasons stated above.
Then we multiply the two arrays together to get the result.
Example: nums = [2, 3, 4, 5]
left = [1, 2, 6, 24]
right = [60, 20, 5, 1]
answer = [60, 40, 30, 24]
The above solution uses three loops. This can be reduced to two loops by combining the last two loops.
The question then asks for an optimal solution of "Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)"
We can build on the above solution. The optimized solution is similar to the above solution with the following changes:
The left and right arrays are removed
The first loop remains the same, except the left array is replaced with the answer array
For the second loop, we still want to iterate backward over the nums array. But now we are condensing the creation of the right array and multiplying it with the answer array in this second loop. The tally variable is used keep the running product of the nums array (this is the right side calculation).