#C11807. Product Except Self
Product Except Self
Product Except Self
Given an array of integers, compute a new array such that for each index i
, the element at that index is the product of all the numbers in the original array except the one at index i
. In other words, for an input array \(A = [a_0, a_1, \ldots, a_{n-1}]\), you need to construct an output array \(B = [b_0, b_1, \ldots, b_{n-1}]\) such that:
[ b_i = \prod_{\substack{0 \leq j < n \ j \neq i}} a_j, ]
Note: Do not use division in your solution because the array may contain zeroes. Instead, consider computing the products from the left and right sides separately.
The input will be provided via standard input and the output should be printed to standard output.
inputFormat
The first line of input contains a single integer n
— the number of elements in the array. If n > 0
, the second line contains n
space-separated integers representing the elements of the array.
If n = 0
then the array is empty.
outputFormat
Print a single line containing n
space-separated integers, where the i-th
integer is the product of all array elements except the element at index i
. For the case of an empty array (n = 0
), print nothing.
4
1 2 3 4
24 12 8 6