-
Notifications
You must be signed in to change notification settings - Fork 2
/
array_product_except_ith_element.cpp
71 lines (55 loc) · 1.45 KB
/
array_product_except_ith_element.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Given an array A of size N, construct a Product Array P (of same size) such that P is equal to the product of all the elements of A except A[i].
Input:
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains two lines of input. The first line is N. The second line contains N elements separated by spaces.
Output:
For each testcase, print the Product array P.
Constraints:
1 <= T <= 10
1 <= N <= 1000
1 <= Ai <= 20
Example:
Input:
2
5
10 3 5 6 2
2
12 20
Output:
180 600 360 300 900
20 12
Explanation:
Testcase1: For the product array P, at i=0 we have 3*5*6*2. At i=1 10*5*6*2. At i=2 we have 10*3*6*2. At i=3 we have 10*3*5*2. At i=4 we have 10*3*5*6
So P is 180 600 360 300 900
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
int left[n], right[n], res[n];
int prod=1;
left[0]=1;
right[n-1]=1;
for(int i=1;i<n;i++)
left[i]=a[i-1]*left[i-1];
for(int i=n-2;i>=0;i--)
right[i]=a[i+1]*right[i+1];
res[0]=right[0];
res[n-1]=left[n-1];
for(int i=1;i<n-1;i++)
res[i]=left[i]*right[i];
for(int i=0;i<n;i++)
cout<<res[i]<<" ";
cout<<endl;
}
return 0;
}