-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductExceptSelf.cpp
More file actions
72 lines (65 loc) · 1.16 KB
/
ProductExceptSelf.cpp
File metadata and controls
72 lines (65 loc) · 1.16 KB
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
72
#include<iostream>
#include<vector>
using namespace std;
void productExceptSelf(vector<int>& nums)
{
long int prod = 1;
bool isZero = 0;
bool allZero = 1;
int count = 0;
for(int i=0; i<nums.size(); i++)
{
if(nums[i])
{
prod*=nums[i];
count++;
}
else
isZero = 1;
}
if(count > nums.size()-2)
allZero = 0;
vector<int> ans;
if(allZero)
{
int n = nums.size();
while(n--)
ans.push_back(0);
}
else if(isZero)
{
for(int i=0; i<nums.size(); i++)
{
if(nums[i])
{
ans.push_back(0);
}
else
{
ans.push_back(prod);
}
}
}
else
{
for(int i=0; i<nums.size(); i++)
{
int n = prod/nums[i];
ans.push_back(n);
}
}
for(int i=0; i<ans.size(); i++)
cout << ans[i] << " ";
}
int main()
{
vector<int> v;
int x;
for(int i=0; i<6; i++)
{
cin >> x;
v.push_back(x);
}
productExceptSelf(v);
}
// LC: Q.238