Given an integer array
The product of any prefix or suffix of
You must write an algorithm that runs in
Output: [24,12,8,6]
Output: [0,0,9,0,0]
Follow up:
Can you solve the problem inContents
Solution 1 - using prefix and postfix arrays Solution 2 - using prefix and postfix arrays approach without really creating them (Answer to the follow-up question)
In this approach, what we are going to do is, build two separate arrays
Once we have
Consider an example,
-
We will create
prefix and fill it with prefix product values:for(int i=0; i<nums.length; i++) { prefix[i] = nums[i] * (i ==0 ? 1 : prefix[i-1]); } prefix array will look like this[1, 2, 6, 24] -
We will create
postfix and fill it with postfix product values:for(int i=nums.length -1; i>=0; i--) { postfix[i] = nums[i] * (i==nums.length-1 ? 1 : postfix[i+1]); } postfix array will look like this[24, 24, 12, 4] -
Once we have the
prefix andpostfix arrays, then if we want to compute product of entire array except self, then we should take the product of all values from left side of array before current element, which we can find it fromprefix array ati-1th index and product of all values from right side of array after current element, which we can find it frompostfix array ati+1th index.int[] ans = new int[nums.length]; for(int i=0 ;i< nums.length; i++) { ans[i] = (i==0 ? 1 : prefix[i-1]) * (i == nums.length-1 ? 1 : postfix[i+1]); } ans array index prefix postfix ans 0 1 24 1 (because i== 0) * 24 = 241 2 24 1 * 12 = 12 2 6 12 2 * 4 = 8 3 24 4 6 * 1 (because i== nums.length -1) = 6
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(n) for the
In this approach, we are going to use the same solution as we saw in previous approach, but without really creating
We are going to create the
-
One thing that we do differently in this case is, instead of storing the prefix product values, we compute prefix product and store that at the next index. This is because we will be using product until index
i ati+1 th index from prefix product.For example say if the input array has elements like this
[0......k, k+1,......n] when we compute prefix product at0 , that will be used at index1 and similarly the prefix product untilk will be used atk+1 .For example, if the input array is
nums = [1, 2, 3, 4] , then we will fill theans like this:ans array prefix index nums ans 0 1 1 (we will place 1 here, as it has no impact on product) 1 2 nums[i-1] * ans[i-1] = 1 * 1 = 1 2 3 2 * 1 = 2 3 4 3 * 2 = 6 If you notice
ans array after prefix values calculated and placed at thr right indexes, last element in theans array will be the right value for that index (product of all except self). -
In the next step, we will start from right to left from index
nums.length-2 to index0 and compute the postfix and multiply that with current element inans array.ans array after postfix index nums ans (before prefix) ans (after prefix) 0 1 1 postfix = 12 * (nums[i+1]) = 12 * 2 = 24
ans[i] * postfix = 1 * 24 = 241 2 1 postfix = 4 * (nums[i+1]) = 4 * 3 = 12
ans[i] * postfix = 1 * 12 = 122 3 2 postfix = 4 (nums[i+1])
ans[i] * postfix = 2 * 4 = 83 4 6 6 (we dont touch it, as it is already computed correctly in prefix array)
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1) if we dont consider
Above implementations source code can be found at