#K47997. Product of Array Except Self

    ID: 28322 Type: Default 1000ms 256MiB

Product of Array Except Self

Product of Array Except Self

Given an array of integers, your task is to compute a new array such that each element at index i is equal to the product of all the elements of the original array except the one at index i. In other words, for an input array A, the output array B should satisfy

$$B_i = \prod_{\substack{j=0 \\ j \neq i}}^{n-1} A_j$$

You must solve the problem with a time complexity of O(n) and without using division.

inputFormat

The input is read from stdin and has the following format:

  • The first line contains an integer n representing the number of elements in the array.
  • The second line contains n space-separated integers.

outputFormat

Print to stdout a single line containing n space-separated integers representing the resulting product array. If the array has one or no elements, print an empty line.

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