Skip to content

Commit e27a41b

Browse files
authored
Create find-the-maximum-divisibility-score.cpp
1 parent dc15a45 commit e27a41b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n * d)
2+
// Space: O(1)
3+
4+
// brute force
5+
class Solution {
6+
public:
7+
int maxDivScore(vector<int>& nums, vector<int>& divisors) {
8+
vector<int> result = {-1, -1};
9+
for (const auto& d: divisors) {
10+
const int curr = accumulate(cbegin(nums), cend(nums), 0, [&](const auto& total, const auto& x) {
11+
return total + static_cast<int>(x % d == 0);
12+
});
13+
if (curr > result[0] || curr == result[0] && d < result[1]) {
14+
result = {curr, d};
15+
}
16+
}
17+
return result[1];
18+
}
19+
};

0 commit comments

Comments
 (0)