#K5501. Product Array Puzzle
Product Array Puzzle
Product Array Puzzle
In this problem, you are given an array (A) of (n) integers. Your task is to construct a new array (B) such that each element (B[i]) is equal to the product of all the elements of (A) except (A[i]). In other words, for each valid index (i),
[ B[i] = \prod_{\substack{j=0 \ j \neq i}}^{n-1} A[j] ]
Note that the input begins with an integer (T) representing the number of test cases. For each test case, the first line contains an integer (n) (the number of elements in the array) followed by a line containing (n) space-separated integers. Your solution should output (T) lines, where each line contains the corresponding product array with its elements separated by a space.
inputFormat
The input is read from standard input (stdin
) and has the following format:
- An integer
T
denoting the number of test cases. - For each test case:
- An integer
n
representing the number of elements in the array. - A line containing
n
space-separated integers.
- An integer
It is guaranteed that the input follows this format.
outputFormat
The output should be written to standard output (stdout
) and contain T
lines. Each line corresponds to a test case and contains n
space-separated integers. The i-th
integer in a line must be the product of all input array elements except the element at index i
.
3
1 2 3
6 3 2