#C8994. Product Except Self
Product Except Self
Product Except Self
Given an array of n integers, your task is to compute an output array such that each element at index i is equal to the product of all elements in the input array except the one at i. You must not use division and your solution should run in O(n) time complexity.
The relation for each element is given by the formula:
\( output[i] = \Bigl( \prod_{j=0}^{i-1} nums[j] \Bigr) \times \Bigl( \prod_{j=i+1}^{n-1} nums[j] \Bigr) \)
For example, if the input array is [1, 2, 3, 4], the output array should be [24, 12, 8, 6].
inputFormat
The input is read from stdin and follows the format below:
- The first line contains an integer
n
, which is the number of elements in the array. - The second line contains
n
space-separated integers representing the elements of the array.
outputFormat
The output is written to stdout as a single line consisting of n
space-separated integers. The i-th
integer in the output should be the product of all elements of the input array except the element at index i
.
4
1 2 3 4
24 12 8 6