- Related Questions & Answers
- Count numbers whose difference with N is equal to XOR with N in C++
- Count numbers whose XOR with N is equal to OR with N in C++
- Count the numbers < N which have equal number of divisors as K in C++
- Count smaller numbers whose XOR with n produces greater value in C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Count subarrays whose product is divisible by k in C++
- Count of subarrays whose maximum element is greater than k in C++
- Count all the numbers less than 10^6 whose minimum prime factor is N C++
- Count numbers upto N which are both perfect square and perfect cube in C++
- Count Primes in Ranges in C++
- Count Primes in Python
- Count numbers whose sum with x is equal to XOR with x in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Maximum Primes whose sum is equal to given N in C++
- Counting numbers whose difference from reverse is a product of k in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given two integers N and K, the goal is to find the count of numbers such that they follow below conditions −

Number<=N

| Number−count | >=K Where count is the number of prime numbers less than or equal to Number.

**For Example**

N = 5, K = 2

Count of numbers < = N whose difference with the count of primes upto them is > = K are: 2

The numbers that follow the conditions are: 5 ( 5−2>=2 ) and 4 ( 4−2>=2 )

N = 10, K = 6

Count of numbers < = N whose difference with the count of primes upto them is > = K are: 1

The numbers that follow the conditions are: 10 ( 10−4>=6 )

**Approach used in the below program is as follows** −

In this approach we will use binary search to reduce our calculations. If the count of prime numbers upto num is count1 and for number num+1 this count is count2. Then the difference num+1−count2 >= num−count1. So if num is valid, num+1 will also be valid.For the first number found, say ‘num’ using binary search that follows the condition, then ‘num’+1 would also follow the same condition. In this way all the numbers between num to N will be counted.

Take variables N and K as input.

Array arr[] is used to store the count of prime numbers upto i will be stored at index i.

Function set_prime() updates array arr[] for storing the counts of primes.

Array check[i] stores true if i is prime else stores false.

Set check[0]=check[1] = false as they are non primes.

Traverse check from index i=2 to i*i<size(1000001). And if any check[i] is 1, number is prime then set all check[j] with 0 from j=i*2 to j<size.

Now traverse arr[] using for loop and update it. All counts upto arr[i]=arr[i−1]. If arr[i] itself is prime then that count will increase by 1. Set arr[i]++.

Function total(int N, int K) takes N and K and returns Count of numbers < = N whose difference with the count of primes upto them is > = K.

Call set_prime().

Take temp_1=1 and temp_2=N. Take the initial count as 0.

Now using binary search, in while loop take set = (temp_1 + temp_2) >> 1 ((first+last) /2 ).

If set−arr[set] is >=K then condition is met, update count with set and temp_2=set−1.

Otherwise set temp_1=temp_1+1.

At the end set count as minimum valid number N−count+1 or 0.

At the end of all loops return count as result.

#include <bits/stdc++.h> using namespace std; #define size 1000001 int arr[size]; void set_prime(){ bool check[size]; memset(check, 1, sizeof(check)); check[0] = 0; check[1] = 0; for (int i = 2; i * i < size; i++){ if(check[i] == 1){ for (int j = i * 2; j < size; j += i){ check[j] = 0; } } } for (int i = 1; i < size; i++){ arr[i] = arr[i − 1]; if(check[i] == 1){ arr[i]++; } } } int total(int N, int K){ set_prime(); int temp_1 = 1; int temp_2 = N; int count = 0; while (temp_1 <= temp_2){ int set = (temp_1 + temp_2) >> 1; if (set − arr[set] >= K){ count = set; temp_2 = set − 1; } else { temp_1 = set + 1; } } count = (count ? N − count + 1 : 0); return count; } int main(){ int N = 12, K = 5; cout<<"Count of numbers < = N whose difference with the count of primes upto them is > = K are: "<<total(N, K); return 0; }

If we run the above code it will generate the following output −

Count of numbers < = N whose difference with the count of primes upto them is > = K are: 4

Advertisements