#C7030. Product Except Self
Product Except Self
Product Except Self
You are given an integer array \(nums\) of size \(n\). Your task is to return an array \(answer\) such that \(answer[i]\) is equal to the product of all the elements of \(nums\) except \(nums[i]\).
You must solve the problem without using division and in \(O(n)\) time.
Example:
Input: nums = [1, 2, 3, 4] Output: [24, 12, 8, 6] Explanation: For index 0: product = 2*3*4 = 24 For index 1: product = 1*3*4 = 12 For index 2: product = 1*2*4 = 8 For index 3: product = 1*2*3 = 6
Hint: Consider using two auxiliary arrays to store cumulative products from the left and from the right.
inputFormat
The first line contains an integer \(T\) representing the number of test cases.
For each test case, the first line contains an integer \(n\) which is the size of the array. The second line contains \(n\) space-separated integers representing the elements of the array \(nums\).
Input is read from standard input (stdin).
outputFormat
For each test case, output a single line containing \(n\) space-separated integers. Each integer should represent the product of all elements of \(nums\) except the one at the current index.
Output is printed to standard output (stdout).
## sample2
3
1 2 3
4
4 5 6 7
6 3 2
210 168 140 120
</p>