1 / 8
Aug 2016

i had submitted this solution to the problem PALIN - The Next Palindrome, but it hasnt been accepted. can anybody help me out here?

include

using namespace std;

bool palin(int);
int main()
{
int arr[10],i,n,m;
cin>>m;

for(i=0;i<m;i++)
{
    cin>>arr[i];
}

cout<<"\n";

for(i=0;i<m;i++)
{
      n=i;
      while (palin(++arr[i])==false);
      cout<<arr[i]<<endl;
      i=n;
}

return 0;
}

bool palin(int num)
{
int d, check, rev=0;
check=num;
do{
d=num%10;
rev= (rev*10)+d;
num=num/10;
}while(num!=0);

if(check==rev)
    return true;
else
    return false;

}

  • created

    Aug '16
  • last reply

    May '20
  • 7

    replies

  • 3.8k

    views

  • 7

    users

  • 2

    likes

Check the constraints given in the problem statement and choose an appropriate data type.

include

using namespace std;

bool palin(int);
int main()
{
int num,i,n,m;
cin>>num;
while(palin(num)==false){
num++;
}
cout<<num;
return 0;
}

bool palin(int num)
{
int d, check, rev=0;
check=num;
do{
d=num%10;
rev= (rev*10)+d;
num=num/10;
}while(num!=0);
if(check==rev)
return true;
else
return false;

}

include

using namespace std;

bool palin(int);
int main()
{
int num,i,n,m;
cin>>num;
while(palin(num)==false){
num++;
}
cout<<num;
return 0;
}

bool palin(int num)
{
int d, check, rev=0;
check=num;
do{
d=num%10;
rev= (rev*10)+d;
num=num/10;
}while(num!=0);
if(check==rev)
return true;
else
return false;

}

1 year later

I get the correct answer on my code
but when i submit this solution it gives me wrong answer

#include <stdio.h>

int main(void) {
int n,temp,x,test;
scanf("%d",&test);
while(test–)
{
scanf("%d",&n);
n++;
while(n<1000000)
{
temp=n;
x=0;
while(n)
{
x=x*10+n%10;
n=n/10;
}
if(temp==x)
{
printf("%d\n",temp);
break;
}
n=++temp;
}
}
return 0;
}

Check the problem statement for the maximum size of K, then search the forum where it’s been answered before.

5 months later

i think this should work but unfortunately i’m unable to find a test case where my code fails
#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll length(ll n)
{
ll count=0;
while(n)
{
n/=10;
count++;
}
return count;
}

int main()
{
int t;
cin>>t;
while(t–)
{
ll n;
cin>>n;
ll len = length(n);
ll half = len/2;
ll new_num =n;
while(half)
{
new_num/=10;
half–;
}

	ll ans1 =new_num,ans2 = ceil(len/2.0)<length(new_num+1)?(new_num+1)/10:new_num+1;
//	cout<<"ans2"<<" "<<ans2<<endl;
	if(len%2==0)
	{
		ll a = new_num;
		while(a)
		{
			ans1 = ans1*10+ a%10;
			a/=10;
		}
		a=new_num+1;
		while(a)
		{
			ans2 = ans2*10+ a%10;
			a/=10;
		}
	}
	else
	{
		ll a = new_num/10;
		while(a)
		{
			ans1 = ans1*10+ a%10;
			a/=10;
		}
		a= (new_num+1)/10;
		while(a)
		{
			ans2= ans2*10+a%10;
			a/=10;	
		}	
	}	
	if(ans1<ans2 and ans1>n)
		cout<<ans1<<endl;
	else
		cout<<ans2<<endl;
}

}

1 year later

I am using this approach, but showing time limit exceeded.
#include<bits/stdc++.h>
//#define _OPEN_SYS_ITOA_EXT
//#include <stdio.h>
//#include <stdlib.h>
using namespace std;
bool palin(int num )
{
string s ;
s = to_string(num) ;
int l = 0;
int r = s.length() - 1;
while(l < r)
{
if(s[l] != s[r])
return false ;
l++ ;
r–;
}
return true ;
}
int main() {

// your code here

int t ; 
cin >> t ; 
while(t--)
{
	 string s ; 
	cin >> s; 
 	//string s = to_string(num) ; 
 	int l = 0; 
 	int r = s.length() -1; 
 	while( l <= r)
 	{
 		if(s[l] == s[r] && s[r] == '9')
 		{
 			l++; 
 			r--; 
 		}
 		else 
 		break;
 	}
 	if(l > r)
 	{
 		string ans1 ;
 		char one = '1' ; 
 		ans1.push_back(one) ; 
 		char zero = '0' ;
 		for(int i = 0; i < s.length() -1;i++)
 		ans1.push_back(zero) ; 
 		ans1.push_back(one) ; 
 		
 		cout << ans1 ; 
 		if(t > 0)
 		cout << endl ;
 		continue ; 
 		
 	}
 	string ans = s ;
 	l = 0; 
 	r = s.length() - 1; 
 //	cout << ans << endl ;
 	while(l < r)
 	{
 		ans[r] = s[l] ; 
 		r--; 
 		l++ ; 
 	}
 	//cout << ans << endl ; 
 	  int n = s.length() ; 
 	if(ans <= s)
 	{
 	  
 	    if(n%2 != 0 )
 	    {
 	      int h = n/2 ; 
 	      if(ans[h] != '9')
 	      {
 	          int k = ans[h] - '0' ; 
 	          k++ ; 
 	          ans[h] = k + '0' ; 
 	      }
 	      else 
 	      ans[h] = '0' ; 
 	    }
 	    l = n/2 - 1; 
 	    if(n%2 == 0 )
 	    r = n/2 ; 
 	    else 
 	    r = n/2 + 1; 
 	    while( l != 0 &&  ans <= s)
 	    {
 	        if(ans[l] != '9')
 	        {
 	            int k = ans[l] - '0' ; 
 	            k++ ; 
 	          ans[r] = ans[l] = k + '0' ; 
 	        }
 	        else 
 	        ans[r] = ans[l] = '0' ; 
 	        l--; 
 	        r++ ; 
 	        
 	    }
 	}
 	 
 	 cout << ans; 
 	 if(t > 0 ) 
 	 cout << endl ; 
 	
 	
	
	 
}
return 0;

}