#K7061. 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. The solution must run in O(n) time and should not use the division operation.
More formally, for an input array \(a = [a_0, a_1, \dots, a_{n-1}]\), output an array \(b = [b_0, b_1, \dots, b_{n-1}]\) where
\[
b_i = \prod_{\substack{0 \leq j < n \\ j \neq i}} a_j
\]
for each \(0 \leq i < n\).
Note that if the array has only one element, output [1].
inputFormat
The first line contains a single integer \(n\) which represents the number of elements in the array. The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output a single line containing \(n\) space-separated integers, where the \(i^{th}\) integer is the product of all numbers in the input array except the one at index \(i\).
## sample4
1 2 3 4
24 12 8 6