#K78737. Product Except Self

    ID: 35153 Type: Default 1000ms 256MiB

Product Except Self

Product Except Self

Given an array of integers, 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\). The solution should be completed in O(n) time and must not use division. In other words, for an input array \(nums\) of size \(n\), you need to generate an output array \(result\) where:

[ result[i] = \prod_{\substack{0 \leq j < n \ j \neq i}} nums[j] ]

Examples:

  • For nums = [1, 2, 3, 4, 5], the output should be [120, 60, 40, 30, 24].
  • For nums = [3, 2, 1], the output should be [2, 3, 6].

inputFormat

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

outputFormat

Output a single line with \(n\) space-separated integers, representing the new array where each element is the product of all the elements in the original array except the element at that position.

## sample
5
1 2 3 4 5
120 60 40 30 24