#C6312. Product of Array Except Self
Product of Array Except Self
Product of Array Except Self
Given an array of integers, compute a new array result such that each element result[i]
is equal to the product of all the elements in the original array except nums[i]
. The solution must be implemented without using the division operator.
The mathematical formulation is given by:
$$result[i] = \prod_{\substack{j=0 \\ j \neq i}}^{n-1} nums[j] $$Handle edge cases where the array might be empty or contains a single element. In the case of a single-element array, output 1
as the product of no elements is considered to be 1.
inputFormat
The input is given through standard input (stdin). The first line contains a non-negative integer n
(0 ≤ n ≤ 105) representing the number of elements in the array. The second line contains n
space-separated integers.
outputFormat
Output a single line to standard output (stdout) containing n
space-separated integers, where each integer corresponds to the product of all elements in the input array except the element at that index.
4
1 2 3 4
24 12 8 6