-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCountNonDivisible.cpp
46 lines (38 loc) · 1.11 KB
/
CountNonDivisible.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
// https://app.codility.com/programmers/lessons/11-sieve_of_eratosthenes/count_non_divisible/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(N*log(N))
//
#include <iostream>
#include <map>
#include <vector>
using namespace std;
vector<int> solution(vector<int> &A) {
const int N = A.size();
map<int, int> divisors;
// Get list of possible divisors, map will sort it
// This is worst case O( N*log(N) )
for (const auto &a: A) {
divisors[a]++;
}
// Go through and calculate the number of divisors that are missing
// We know that there are at least N nondivisors present if none
// of the divisors are present. Making this +1 so I don't have to use
// zero based indices
vector<int> track((2*N)+1, N);
// Generate divisors list
// This is the Sieve of Eratosthenes method
for (const auto &d: divisors) {
for (int i=1; i*d.first <= 2*N; i++) {
track[i*d.first] -= d.second;
}
}
// Look up nondivisors
vector<int> nondivisors(N, 0);
for (int i=0; i<N; i++) {
nondivisors[i] = track[A[i]];
}
return nondivisors;
}