#C13465. Product of Array Except Self

    ID: 43006 Type: Default 1000ms 256MiB

Product of Array Except Self

Product of Array Except Self

Given an array of integers of size \( n \), compute a new array such that each element at index \( i \) is equal to the product of all the numbers in the original array except the one at \( i \). You are not allowed to use division in your solution.

For example, if the array is [1, 2, 3, 4], the expected output is [24, 12, 8, 6] because:

[ \text{result}[0] = 2 \times 3 \times 4 = 24, \ \text{result}[1] = 1 \times 3 \times 4 = 12, \ \text{result}[2] = 1 \times 2 \times 4 = 8, \ \text{result}[3] = 1 \times 2 \times 3 = 6. ]

The input array can contain zero or negative numbers. In the case of a single element, output 1, as there are no other numbers to multiply.

inputFormat

The first line contains an integer \( n \) representing the number of elements in the array. If \( n > 0 \), the second line contains \( n \) space-separated integers.

Note: If \( n = 0 \), it represents an empty array and no further input is given.

outputFormat

Output a single line containing \( n \) space-separated integers where the \( i^{th} \) integer is the product of all array elements except the one at index \( i \). If \( n = 0 \), output nothing.

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