Given two positive integers N and K, the task is to count all the numbers that satisfy the following conditions:
If the number is num,
- num ≤ N.
- abs(num – count) ≥ K where count is the count of primes upto num.
Input: N = 10, K = 3
6, 7, 8, 9 and 10 are the valid numbers. For 6, the difference between 6 and prime numbers upto 6 (2, 3, 5) is 3 i.e. 6 – 3 = 3. For 7, 8, 9 and 10 the differences are 3, 4, 5 and 6 respectively which are ≥ K.
Input: N = 30, K = 13
Prerequisite: Binary Search
Approach: Observe that the function which is the difference of the number and count of prime numbers upto that number is a monotonically increasing function for a particular K. Also, if a number X is a valid number then X + 1 will also be a valid number.
Let the function Ci denotes the count of prime numbers upto number i. Now,
for the number X + 1 the difference is X + 1 – CX + 1 which is greater than
or equal to the difference X – CX for the number X, i.e. (X + 1 – CX + 1) ≥ (X – CX).
Thus, if (X – CX) ≥ S, then (X + 1 – CX + 1) ≥ S.
Hence, we can use binary search to find the minimum valid number X and all the numbers from X to N will be valid numbers. So, the answer would be N – X + 1.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of numbers whose difference with Fibonacci count upto them is atleast K
- Count of all values of N in [L, R] such that count of primes upto N is also prime
- Count primes that can be expressed as sum of two consecutive primes and 1
- Count of primes below N which can be expressed as the sum of two primes
- Count of interesting primes upto N
- Length of largest sub-array having primes strictly greater than non-primes
- Nth Term of a Fibonacci Series of Primes formed by concatenating pairs of Primes in a given range
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of numbers upto M divisible by given Prime Numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Split N into two integers whose addition to A and B makes them equal
- Maximum Primes whose sum is equal to given N
- Sort numbers based on count of letters required to represent them in words
- Count pairs in an array such that the absolute difference between them is ≥ K
- Count the pairs in an array such that the difference between them and their indices is equal
- Count numbers which can be represented as sum of same parity primes
- Count numbers upto N which are both perfect square and perfect cube
- Count of Octal numbers upto N digits
- Count of numbers upto M with GCD equals to K when paired with M
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.