#K92352. Product of Array Except Self
Product of Array Except Self
Product of Array Except Self
Given an integer array \(nums\), compute an output array \(answer\) such that for each index \(i\), \(answer[i]\) is equal to the product of all the elements of \(nums\) except \(nums[i]\). You must solve this without using division.
The solution can be implemented by computing prefix products and suffix products. In other words, define:
\(prefix[i] = \prod_{j=0}^{i-1} nums[j]\) and \(suffix[i] = \prod_{j=i+1}^{n-1} nums[j]\). Then, \(answer[i] = prefix[i] \times suffix[i]\).
This problem tests your ability to manipulate arrays and perform efficient computations under constraints.
inputFormat
The first line of input contains a single integer \(n\) (\(1 \leq n \leq 10^5\)), the number of elements in the array. The second line contains \(n\) space-separated integers representing the elements of the array \(nums\).
outputFormat
Output a single line containing \(n\) space-separated integers, where the \(i\)-th integer represents the product of all elements in the array except the one at index \(i\).
## sample5
1 2 3 4 5
120 60 40 30 24