Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i].

The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.

You must write an algorithm that runs in O(n) time and without using the division operation.

Input: nums = [1,2,3,4]
Output: [24,12,8,6]
Input: nums = [-1,1,0,-3,3]
Output: [0,0,9,0,0]

Follow up:
Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)

Contents

In this approach, what we are going to do is, build two separate arrays prefix array and postfix array and populate these array with prefix product values from left to right into prefix array and right to left into postfix array.

Once we have prefix array and postfix array, then the product value of entire array except the element at index i will be the product of the values prefix[i-1] and postfix[i+1], and for the edge case scenarios where i is either 0 or end of array we can take these values as 1. ans[i] = (i==0 ? 1 : prefix[i-1]) * (i==nums.length-1 ? 1 : postfix[i+1]);

Consider an example, nums = [1, 2, 3, 4]

public class ProductOfArrayExceptSelf { /** * Using prefix and postfix arrays */ static int[] usingPrefixAndPostfixArrays(int[] nums) { int[] prefix = new int[nums.length]; int[] postfix = new int[nums.length]; for(int i=0; i<nums.length; i++) { prefix[i] = nums[i] * (i ==0 ? 1 : prefix[i-1]); } for(int i=nums.length -1; i>=0; i--) { postfix[i] = nums[i] * (i==nums.length-1 ? 1 : postfix[i+1]); } 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]); } return ans; } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of input array nums.
Space complexity: O(n) for the prefix and postfix arrays.

In this approach, we are going to use the same solution as we saw in previous approach, but without really creating prefix and postfix array.

We are going to create the ans array and use that to store the prefix and postfix product values directly into this array:

public class ProductOfArrayExceptSelf { /** * * we are going to use answer array itself to store prefix array values, * then multiply those with postfix product */ static int[] usingPrefixAndPostfixProductMemoryOptimized(int[] nums) { int[] ans = new int[nums.length]; // building prefix product values and placing them at i+1, // as this is where we would need i.....k product at k+1 (th) index ans[0] = 1; for(int i=1; i<nums.length; i++) { ans[i] = nums[i-1] *ans[i-1]; } // compute post fix and multiply that with ans[i] int postfix = 1; for(int i=nums.length -2; i>=0; i--){ postfix = postfix * nums[i+1]; ans[i] = ans[i] * postfix; } return ans; } }
Complexity Analysis:

Time complexity: Above code runs in O(n) time where n is the length of input array nums.
Space complexity: O(1) if we dont consider ans output array.

Above implementations source code can be found at GitHub link for Java code