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&nbsp;O(n)&nbsp;time and without using the division operation.

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

&nbsp;
Constraints:


	2 &lt;= nums.length &lt;= 105
	-30 &lt;= nums[i] &lt;= 30
	The product of any prefix or suffix of nums is guaranteed to fit in a 32-bit integer.


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

class Solution:
    def productExceptSelf(self, nums: List[int]) -> List[int]:
        pre = [1]* (len(nums)+1)
        back = [1]*(len(nums)+1)

        for i in range(len(nums)):
            pre[i+1] = pre[i]*nums[i]

        for i in range(len(nums)-1,-1,-1):
            back[i-1] = back[i]*nums[i]
        for i in range(len(pre)-1):
            nums[i]=pre[i]*back[i]

        return nums
class Solution {
    public int[] productExceptSelf(int[] nums) {
        int n = nums.length;
        int pre[] = new int[n];
        int suff[] = new int[n];
        pre[0] = 1;
        suff[n - 1] = 1;
        
        for(int i = 1; i < n; i++) {
            pre[i] = pre[i - 1] * nums[i - 1];
        }
        for(int i = n - 2; i >= 0; i--) {
            suff[i] = suff[i + 1] * nums[i + 1];
        }
        
        int ans[] = new int[n];
        for(int i = 0; i < n; i++) {
            ans[i] = pre[i] * suff[i];
        }
        return ans;
    }
}
class Solution {
public:
    vector<int> productExceptSelf(vector<int>& nums) {
        int zero=0,product=1;
        for(auto i:nums){
            if(i==0)
                zero++;
            else product*=i;
        }
        for(int i=0;i<nums.size();i++){
            if(nums[i] == 0 && zero>1){
                nums[i]=0;
            }
            else if(nums[i] == 0){
                nums[i]=product;
            }
            else if(zero > 0){
                nums[i]=0;
            }
            else nums[i]=product/nums[i];
        }
        return nums;
    }
};
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var productExceptSelf = function(nums) {
  let result = [];
  let len = nums.length;

  let prefix = 1;
  for (let i = 0; i < len; i++) {
    result[i] = prefix;
    prefix *= nums[i];
  }
  let postfix = 1;
  for (let i = len - 1; i >= 0; i--) {
    result[i] *= postfix;
    postfix *= nums[i];
  }
  return result;
};
Product of Array Except Self