#C13298. Product Except Self

    ID: 42820 Type: Default 1000ms 256MiB

Product Except Self

Product Except Self

Given an integer array \(A\) of length \(n\), your task is to compute an output array \(B\) such that each element \(B_i\) is equal to the product of all elements of \(A\) except \(A_i\). Formally, the formula is given by:

$$ B_i = \prod_{\substack{0 \le j < n \\ j \neq i}} A_j $$

The solution should run in \(O(n)\) time without using division. For example, if \(A = [1, 2, 3, 4]\), then \(B = [24, 12, 8, 6]\). The array may be empty or contain a single element (in which case output \(1\) for that element).

inputFormat

The first line contains a single integer \(n\) (\(0 \le n \le 10^5\)), the number of elements in the array. The second line contains \(n\) integers separated by spaces.

outputFormat

Output a single line containing \(n\) integers separated by spaces, where the \(i\)th integer represents the product of all the numbers in the input array except the one at index \(i\).

## sample
4
1 2 3 4
24 12 8 6