LinkedIn – Interview – Find numbers divisible by 3 not 9 (without using remainder and division) | Yaozong's Blog
LinkedIn – Interview – Find numbers divisible by 3 not 9 (without using remainder and division)
Read full article from LinkedIn – Interview – Find numbers divisible by 3 not 9 (without using remainder and division) | Yaozong's Blog
LinkedIn – Interview – Find numbers divisible by 3 not 9 (without using remainder and division)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
vector<int> find_elements_div3_not9(int n)
{
if (n < 3)
return {};
vector<bool> flags(n + 1, false);
flags[3] = true;
int mult = 2;
vector<int> res(1, 3);
while (mult * 3 <= n) {
if (!flags[mult]) {
res.push_back(mult * 3);
}
flags[mult * 3] = true;
mult = mult + 1;
}
return res;
}
|