Given two numbers n and k, find whether there exist at least k **Special** prime numbers or not from 2 to n inclusively.

A prime number is said to be **Special** prime number if it can be expressed as the sum of three integer numbers: two neighboring prime numbers and 1. For example, 19 = 7 + 11 + 1, or 13 = 5 + 7 + 1.

** Note:- **Two prime numbers are called neighboring if there are no other prime numbers between them.

Examples: Input : n = 27, k = 2 Output : YES In this sample the answer is YES since at least two numbers areSpecial13(5 + 7 + 1) and 19(7 + 11 + 1). Input : n = 45, k = 7 Output : NO In this example, theSpecialprime numbers are 13(5 + 7 + 1), 19(7 + 11 + 1), 31(13 + 17 + 1), 37(17 + 19 + 1), 43(19 + 23 + 1). As the no. ofSpecialprime numbers from 2 to 45 is less than k, the output is NO.

To solve this problem we need to find prime numbers in range [2..n]. So we us Sieve of Eratosthenes to generate all the prime numbers from 2 to n. Then, Take every pair of neighboring prime numbers and check if their sum increased by 1 is a prime number too. Count the number of these pairs, compare it to K and output the result.

Below is the CPP implementation of the above approach:-

`// CPP program to check whether there ` `// exist at least k or not in range [2..n] ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `vector<` `int` `> primes; ` ` ` `// Generating all the prime numbers ` `// from 2 to n. ` `void` `SieveofEratosthenes(` `int` `n) ` `{ ` ` ` `bool` `visited[n]; ` ` ` `for` `(` `int` `i = 2; i <= n + 1; i++) ` ` ` `if` `(!visited[i]) { ` ` ` `for` `(` `int` `j = i * i; j <= n + 1; j += i) ` ` ` `visited[j] = ` `true` `; ` ` ` `primes.push_back(i); ` ` ` `} ` `} ` ` ` `bool` `specialPrimeNumbers(` `int` `n, ` `int` `k) ` `{ ` ` ` `SieveofEratosthenes(n); ` ` ` `int` `count = 0; ` ` ` `for` `(` `int` `i = 0; i < primes.size(); i++) { ` ` ` `for` `(` `int` `j = 0; j < i - 1; j++) { ` ` ` ` ` `// If a prime number is Special prime ` ` ` `// number, then we increments the ` ` ` `// value of k. ` ` ` `if` `(primes[j] + primes[j + 1] + 1 ` ` ` `== primes[i]) { ` ` ` `count++; ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If at least k Special prime numbers ` ` ` `// are present, then we return 1. ` ` ` `// else we return 0 from outside of ` ` ` `// the outer loop. ` ` ` `if` `(count == k) ` ` ` `return` `true` `; ` ` ` `} ` ` ` `return` `false` `; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `n = 27, k = 2; ` ` ` `if` `(specialPrimeNumbers(n, k)) ` ` ` `cout << ` `"YES"` `<< endl; ` ` ` `else` ` ` `cout << ` `"NO"` `<< endl; ` ` ` `return` `0; ` `} ` |

Output:-YES

## Recommended Posts:

- Find Largest Special Prime which is less than or equal to a given number
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print the nearest prime number formed by adding prime numbers to N
- Number of special pairs possible from the given two numbers
- Special two digit numbers in a Binary Search Tree
- Numbers less than N which are product of exactly two distinct prime numbers
- Prime numbers after prime P with sum S
- Sum of the first N Prime numbers
- Almost Prime Numbers
- Prime Numbers
- Sum of all the prime numbers in a given range
- XOR of all Prime numbers in an Array
- Prime numbers and Fibonacci
- Check if two numbers are co-prime or not

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.