[Codewars] Consecutive k-Primes

Consecutive k-Primes

  • Time :
  • Space :
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
#include <vector>
using namespace std;
class PrimeConsec
{
private:
static int countPrimeDiv(long long n) {
if(n == 1) return 1;
int res = 0;
for(int i = 2; i * i <= n; i++) {
if(n % i) continue;
while(n % i == 0) {
n /= i;
res += 1;
}
}
if(n != 1) res += 1;
return res;
}
public:
static int consecKprimes(int k, std::vector<long long> &arr) {
for(auto& x : arr) x = countPrimeDiv(x);
int res = 0;
for(int i = 1; i < arr.size(); i++) res += arr[i] == k and arr[i-1] == k;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/05/10/PS/Codewars/consecutive-k-primes/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.