SPALNUM - Editorial

The link provided for author’s and tester’s solution is not valid. Please check

We can also try the below approach:

  1. Start with L, sum=0

  2. Find Next Palindrome Number, //complexity O(Log(10^5))

  3. if PalindromeNumber <= R

          Sum+=PalindromeNumber
    

    else

          return sum
    
  4. Repeat Step 2 // O(10^4)

Total Complexity would be Number of Testcases X 10^4 X O(Log(10^5))

What do you think? For a single test case this should be better solution.

1 Like

Help , Why this code : WIZx1G - Online C++ Compiler & Debugging Tool - Ideone.com give WA even the test case same like the problem , can someone give me some of explanation ?

#include<bits/stdc++.h>
using namespace std;
#define n 312457
int digits[20];
int k=0;
int sum[n]={0};
bool isp(int x)
{
int m,j,k=0;
while(x>0)
{

	digits[k]=x%10;
	x=x/10;
	k++;
}
 for(int i = 0, j = k - 1; i < k; ++i, --j)
{
	if(digits[i]!=digits[j])
	{
		return false;
	}
	
}

	return true;

}

void pre()
{
for(int p=1;p<=1000;p++)
{
if(isp§==true)
{
sum[p]=p;
}
sum[p]+=sum[p-1];
}
}

int main()
{
long long int i,j,k,t,l,r;
cin>>t;
pre();
while(t–)
{

	cin>>l;
	cin>>r;
	cout<<sum[r]-sum[l-1]<<endl;
	
}



















return 0;

}

can we not do this without using array. my code is giving wrong answer.
can someone explain me why.
CodeChef: Practical coding for everyone.
please

#include<stdio.h>
int main()
{
int t,l[100],r[100],i,j,rev=0,sum=0,dig,n;
scanf("%d",&t);
printf("\n");
for(i=0;i<t;i++)
{
scanf("%d %d",&l[i],r[i]);
printf("\n");
}
for(i=0;i<t;i++)
{
sum=0;
if(l[i]<=r[i])
{
for(j=l[i];j<=r[i];j++)
{
l[i]=n;
rev=0;
while(n)
{
dig=n%10;
rev=rev10+dig;
n=n/10;
}
}
if(l[i]==rev)
{
sum=sum+l[i];
}
}
else
{
for(j=r[i];j<=l[i];j++)
{
r[i]=n;
rev=0;
while(n)
{
dig=n%10;
rev=rev
10+dig;
n=n/10;
}
if(r[i]==rev)
{
sum=sum+r[i];
}
}
}
printf("%d\n",sum);
}
return 0;
}

I tried this code but it was good only for subtask 1 and not for subtask 2. Can anyone tell me why?

#include<stdio.h>
#include<string.h>
int main(){
int cases,k;
scanf("%d",&cases);
for(k=0;k<cases;++k){
long int a,b,len,i,sum=0;

	scanf("%li%li",&a,&b);
	while(a<=b){
		char str[8];
		int palin=1;
		
		sprintf(str,"%li",a);
		
		len=strlen(str);
		
		for(i=0;i<=(len/2);++i){
			if(str[i]!=str[(len-1)-i]){
				palin=0;
				break;
			}
		}
		if(palin==1)
			sum+=a;
		
		a++;
	}
	
	printf("%li\n",sum);
}
return 0;

}

I tried this code but it was good only for subtask 1 and not for subtask 2. Can anyone tell me why?

#include<stdio.h>
#include<string.h>
int main(){
int cases,k;
scanf("%d",&cases);
for(k=0;k<cases;++k){
long int a,b,len,i,sum=0;

	scanf("%li%li",&a,&b);
	while(a<=b){
		char str[8];
		int palin=1;
		
		sprintf(str,"%li",a);
		
		len=strlen(str);
		
		for(i=0;i<=(len/2);++i){
			if(str[i]!=str[(len-1)-i]){
				palin=0;
				break;
			}
		}
		if(palin==1)
			sum+=a;
		
		a++;
	}
	
	printf("%li\n",sum);
}
return 0;

}

You can also reverse the number and then compare it with the original number to find out if the number is a palindrome or not.It works for both all the test cases.

is it necessary to take the help of data structrure (array)

why the decimal representation of N have log(N) digits?

Wow! This method is freaking amazing! My program execution time decreased from 1.30 seconds to 0.06 seconds. The change is amazing! Wow!

Can Anyone can explain the following lines, how it works? I’m not getting it. Examples might be used to better explaination. Thanks In Advance.

query [A,B][A,B] equals F[B]−F[A−1]

it is not giving correct answer even for test cases. u can have a look at CodeChef: Practical coding for everyone

i know that but if you look at my program and the approach mentioned above ,you will find that both are exactly same. If anyone can tell where i am wrong.

Practice link redirects to Pattern Matching Problem.

You have not included right limit ‘b’ which is supposed to be included. Question asks you to include both the given numbers in the sum if they are palindromes. Try the testcase 1 11 where your code produces 45 while the correct answer should be 56

its showing run time error. can anyone tell me what is this run time error??

Here, the base of log is not 2, not 10. That’s why.

What’s the name of the technique, you have mentioned? So, that we know more about it using that keyword?