#C8903. Product Except Self
Product Except Self
Product Except Self
Given an array of integers, your task is to return an output array such that each element at index i is equal to the product of all the numbers in the original array except nums[i]. In other words, you need to compute
\( output[i] = \prod_{\substack{j=0 \\ j\neq i}}^{n-1} nums[j] \)
You are not allowed to use division in your solution. The solution should achieve \(O(n)\) time complexity and, excluding the space needed for the output array, \(O(1)\) extra space complexity.
Note that the input array may contain zeros or negative numbers. If the array is empty, the output should be an empty array.
inputFormat
The first line contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers which represent the elements of the array.
outputFormat
Output a single line with \(n\) space-separated integers, where the \(i\)-th integer is the product of all the array elements except the one at index \(i\). If \(n = 0\), output nothing.
## sample4
1 2 3 4
24 12 8 6