1

There is a problem on Pallindrome given in the site www.spoj.com. The problem statement is - For a given positive integer K of not more than 1000000 digits, write the value of the smallest palindrome larger than K to output. Numbers are always displayed without leading zeros. Input

The first line contains integer t, the number of test cases. Integers K are given in the next t lines.

I have written a CPP code for this

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
#include <bits/stdc++.h>
#include<limits>
using namespace std;

int main(){
    int b;
    cin>>b;
    int num[b];
    for(int i=0;i<b;i++){
        cin>>num[i];
        if(num[i]>1000000){
            //cout<<"Enter value less than 1000000"<<endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            //cout<<"You have entered wrong input"<<endl;
            cin>>num[i];
        }
    }
    string a[b];
    for(int i=0;i<b;i++){
        long numb = num[i] + 1;

        if(!(numb>1000000 || num[i] <10 )){
            for(int k=numb; k<1000000; k++){
                stringstream ss;
                ss << k;
                string str=ss.str();
                string ostr = str;
                reverse(str.begin(), str.end());
                if(ostr == str){
                    a[i] = ostr;
                    //cout<<ostr<<endl;
                    break;
                }
            }
            cout<<a[i]<<endl;
        }

    }

}

This is still not an acceptable answer. Where am I missing something. I have tested with lot of test cases and its working.

As per suggestions I changed it to this but still it was not accepted.

#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <sstream>
//#include <bits/stdc++.h>
#include<limits>
using namespace std;

int main(){
    int b;
    cin>>b;
    int num[b];
    for(int i=0;i<b;i++){
        cin>>num[i];
        if(num[i]>1000000){
            //cout<<"Enter value less than 1000000"<<endl;
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(),'\n');
            //cout<<"You have entered wrong input"<<endl;
            cin>>num[i];
        }
    }
    string a[b];
    for(int i=0;i<b;i++){
        long numb = num[i] + 1;

        if(!(numb>1000000 || num[i] <10 )){
            for(int k=numb; k<1000000; k++){
                stringstream ss;
                ss << k;
                string str=ss.str();
                string ostr = str;

                int len = str.length();
                int n=len-1;
                for(int m=0;m<(len/2);m++){
                    //Using the swap method to switch values at each index
                    swap(str[m],str[n]);
                    n = n-1;
                }
                //reverse(str.begin(), str.end());
                if(ostr == str){
                    a[i] = ostr;
                    //cout<<ostr<<endl;
                    break;
                }
            }
            cout<<a[i]<<endl;
        }

    }

}
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
  • 3
    Also I think you confused _"integer K of not more than 1000000 digits"_ with an integer not _larger_ than 1000000. If the numbers can get this big (1 Million digits), you can't use ints (or any primitive datatype for that matter) to store the data, but need a different approach. – Lukas-T Jan 28 '20 at 10:27
  • @churill i think thats the point. I need to check how we can enter that big digit for testing. – Amit Ray Jan 28 '20 at 10:36
  • 1
    Currently you are parsing ints, but an int can hold only 10 digit numbers or so. The only solution will probably be to take input as string and work with the string as it is. – Lukas-T Jan 28 '20 at 10:51

0 Answers0