C++ Count Non Divisible
#include <algorithm>
#include <vector>
std::vector<int> countNonDivisible(const std::vector<int>& a)
{
int size = static_cast<int>(a.size());
std::vector<int> nondivisor(size, 0);
int maxVal = *std::max_element(a.begin(), a.end());
std::vector<int> occurrences(maxVal + 1, 0);
for (int v : a) {
++occurrences[v];
}
for (int k = 0; k < size; ++k) {
int v = a[k];
int count = 0;
long long i = 1;
while (i * i <= v) {
if (v % i == 0) {
count += occurrences[i];
if (v / i != i) {
count += occurrences[v / i];
}
}
++i;
}
nondivisor[k] = size - count;
}
return nondivisor;
}
This counts how often each value appears, then subtracts the divisor matches so you get the non-divisible count for each item.