#C3869. Product of Array Except Self
Product of Array Except Self
Product of Array 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 input array except the one at index (i). Formally, for an input array (a), the output array (b) is defined as (b_i = \prod_{\substack{j=0 \ j\neq i}}^{n-1} a_j). If the array is empty, return an empty array. If the array contains a single element, output [1]. Your task is to implement an efficient algorithm with an overall time complexity of (O(n)) without using division. This problem is common in coding competitions and tests your ability to manipulate arrays and compute cumulative products.
inputFormat
The input consists of two lines. The first line contains a single integer (n), representing the number of elements in the array. The second line contains (n) space-separated integers representing the elements of the array. If (n = 0), the second line will be empty.
outputFormat
Output a single line containing (n) space-separated integers. The (i)-th integer should be the product of all the elements in the array except the element at index (i), i.e., (b_i = \prod_{\substack{j=0 \ j\neq i}}^{n-1} a_j).## sample
4
1 2 3 4
24 12 8 6
</p>