#C13371. Product Except Self
Product Except Self
Product Except Self
Given an array of integers, your task is to compute a new array such that the element at each index is equal to the product of all the numbers in the original array except the one at that index. In other words, for an input array \(nums\) of length \(n\), you need to output an array \(result\) where:
\(result[i] = \prod_{\substack{0 \leq j < n \\ j \neq i}} nums[j]\)
Your solution should not use division and must run in \(O(n)\) time.
inputFormat
The input is given via standard input (stdin). The first line contains an integer (n), the number of elements in the array. The second line contains (n) space-separated integers representing the array.
outputFormat
Output to standard output (stdout) 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).## sample
4
1 2 3 4
24 12 8 6
</p>