Only Premium Users can view the Question
Given a range [L, R] and an integer K, find all the groups in the given range having at least K consecutive composite integers.
Example:
Input:
L = 500, R = 650, K = 10
Output:
510 520 11
524 540 17
620 630 11
Explanation:
Prime number between 500 to 650 are {503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647}.
So between them the consecutive composite number which are greater than K are 510-520, 524-540 and 620-630.
Input:
L = 10, R = 18, K = 2
Output:
14 16 3
Given n and a number, the task is to find the sum of n digit numbers that are divisible by given number.
Examples:
Input:
n = 2, number = 7
Output:
728
There are thirteen n digit numbers that are divisible by 7.
Numbers are : 14+ 21 + 28 + 35 + 42 + 49 + 56 + 63 +70 + 77 + 84 + 91 + 98.
Input:
n = 3, number = 7
Output:
70336
Input:
n = 3, number = 4
Output :
124200
Question 2
Naive Approach
N
numbers and for each number check the divisibility and calculate the sum.(10^N)
In this question, we are not given constraints So it gets passed for the N when its range from 0-8
So we have to come up with the better solution.Efficient Approach
total number
divisible by a given number
. After that applying formula for sum_of_ap will give us the correct solution.Below is the equation that we are going to use to compute the answer where count
is the total number divisible by N :
count/2 * (first-term + last-term)
We can get the first term and last term using pow(10, Number)
and pow(10, Number-1)
.
firstnum = (firstnum - firstnum % X)+X
pow()
functionPseudo Code
int firstnum = pow(10, digit - 1);
int lastnum = pow(10, digit);
firstnum = (firstnum - firstnum % X) + X;
lastnum = (lastnum - lastnum % X);
int count = ((lastnum - firstnum) / X + 1);
cout << ((lastnum + firstnum) * count) / 2;
Question 1
Brute Force
Optimized Approach
Pseudo Code
int n = r;
bool prime[n + 1] = {false};
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++)
{
if (prime[p] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
int count = 0;
for (int i = l; i <= r; i++)
{
if (prime[i] == true)
{
if (count >= K)
cout << (i - count) << " " << i - 1 << " " << count << "\n";
count = 0;
}
else
count++;
}
if (count >= K)
cout << (r - count) << " " << r << " " << count << "\n";
Can somebody please tell me the mistake in this code since this one is failing tc 5 in composite numbers. I've stored the primes in the range l to r and calculated the answer based upon the difference value. I've tried with both including l and r in the range and excluding them, both fail :(
using namespace std;
vector<long long> primes;
vector<long long> sv(100005,1);
void sieve() {
for(long long i=2;i*i<100005;i++) {
if(sv[i]==1) {
for(long long j=i*i;j<100005;j+=i) {
sv[j] = 0;
}
}
}
for(long long i=2;i<100005;i++) {
if(sv[i]==1) primes.push_back(i);
}
}
int main() {
sieve();
long long l,r,k;
cin>>l>>r>>k;
vector<long long> arr;
for(long long i=0;i<primes.size();i++) {
if(primes[i]>l && primes[i]<r) arr.push_back(primes[i]);
}
if(arr.size()>0&&arr[0]-l>=k) {
cout<<l<<" "<<arr[0]-1<<" "<<arr[0]-l<<endl;
}
for(long long i=0;i<(long long)arr.size()-1;i++) {
long long cnt = arr[i+1] - arr[i] - 1;
if(cnt>=k) {
cout<<arr[i]+1<<" "<<arr[i+1]-1<<" "<<cnt<<endl;
}
}
if(arr.size()>0&&r-arr[arr.size()-1]>=k) {
cout<
Used same method. Test 5 failing.
`#include <bits/stdc++.h> using namespace std; typedef __int128_t ll;
string toString(__int128_t num) { string str; do { int digit = num % 10; str = to_string(digit) + str; num = (num - digit) / 10; } while (num != 0); return str; }
int main() {
long long n,x; cin>>n>>x; ll max_num=0,min_num=pow(10LL,n-1); for(ll i=0;i
Checked your solution Just u are missing something that is :
max number is always max_num=pow(10ll,n);
and starting number will be alwaysstart=(min_num/x+1);