#C7272. Product of Array Except Self
Product of Array Except Self
Product of Array Except Self
You are given an array of integers nums
with size n. Your task is to compute an output array result
where each result[i]
is the product of all the elements in nums
except nums[i]
. In other words, for each index i
:
$$result[i] = \prod_{\substack{0 \leq j < n \\ j \neq i}} nums[j]$$
You must solve the problem in O(n) time complexity and without using division operations. The solution should use O(1) extra space complexity (excluding the output array).
Example:
Input: 4\n1 2 3 4 Output: 24 12 8 6
inputFormat
The input is read from standard input 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 representing the elements of the array.
outputFormat
The output should be printed to standard output as n
space-separated integers, where the i-th
integer represents the product of all the elements of the array except the i-th
element.
4
1 2 3 4
24 12 8 6