-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpa4.cpp
More file actions
67 lines (60 loc) · 2.22 KB
/
Copy pathpa4.cpp
File metadata and controls
67 lines (60 loc) · 2.22 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
class Solution {
public:
void small2(vector<int> mod, int &f, int &s) {
int first = INT_MAX;
int second = INT_MAX;
for (int i = 0; i < mod.size(); ++i) {
if (mod[i] < first) {
second = first;
first = mod[i];
}
else if (mod[i] <= second ) {
second = mod[i];
}
}
f = first;
s = second;
}
int maxSumDivThree(vector<int>& nums) {
vector<int> mod1;
vector<int> mod2;
int f,s,x,y;
int sum = 0;
vector<int> sumMinus;
for(int i = 0; i < nums.size(); ++i) {
sum += nums[i];
if (nums[i] %3 == 1) {
mod1.push_back(nums[i]); // holds all nums with reminder 1
}
else if (nums[i] %3 == 2) {
mod2.push_back(nums[i]); // holds all nums with remainder 2
}
}
small2(mod1,x,y);
small2(mod2,f,s);
if(sum %3 ==0) { // no need to alter so we just return
return sum;
}
if(sum %3 ==1) {
if(mod1.size() >= 1) {
sumMinus.push_back(sum - x); // if sum has remainder one we can subtract one element with remainder one
}
if(mod2.size() >= 2) {
sumMinus.push_back(sum - f - s); // if sum has remainder one we can subtract two elements with remainder two
}
}
if(sum %3 ==2) {
if(mod2.size() >= 1) {
sumMinus.push_back(sum - f); // if sum has remainder two we can subtract one element with remainder two
}
if(mod1.size() >= 2) {
sumMinus.push_back(sum - x - y); // if sum has remainder two we can subtract two elements with remainder one
}
}
return *max_element(sumMinus.begin(), sumMinus.end()); // return maximum solution where x mod 3 = 0
}
};