1 / 2
Sep 2021

Hi Please let me know what is the issue in below code.
It is working in IDE I tried with multiple test cases even one with million digits but getting NZEC error in submissions

import java.util.;
import java.lang.
;

class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
sc.nextLine();
while(t!=0){
String n=sc.nextLine();
char c[]=n.toCharArray();
if(c.length>1000000){
continue;
}
boolean ninesFlag=checkAllNines©;

	    if(ninesFlag){
	        String result=calcNinesPalin(c.length-1);
	        System.out.println(result);
	        t--;
	        continue;
	    }
	    int left=c.length/2,right=c.length/2;
	    if(c.length%2==0){
	        left--;
	    }
	    while(left>0&&(c[left]==c[right])){
	        left--;
	        right++;
	    }
	    String result="";
	    if(c[left]>c[right]){
	        if(c.length%2==0)
	         result=n.substring(0,c.length/2)+reverse(n.substring(0,c.length/2));
	        else
	         result=n.substring(0,c.length/2)+c[c.length/2]+reverse(n.substring(0,c.length/2));
	        		    System.out.println(result);

	    }
	    else if(c[left]<c[right]||c[left]==c[right]){
	        if(c.length%2==0){
	        String sub=n.substring(0,c.length/2-1)+String.valueOf(Long.parseLong(n.substring(c.length/2-1,c.length/2))+1);
	         result=sub+reverse(sub);}
	        else{
	        String sub=String.valueOf(Long.parseLong(n.substring(0,c.length/2+1))+1);
             result=sub+reverse(sub.substring(0,sub.length()-1));
	        }
	        System.out.println(result);
	    }
	    t--;
	}
}
public static String reverse(String S){
    StringBuilder sb=new StringBuilder(S);
    sb.reverse();
    return sb.toString();
}
public static boolean checkAllNines(char c[]){
    for(int i=0;i<c.length;i++){
       if(c[i]!='9')
        return false;
    }
    return true;
}
public static String calcNinesPalin(long n){
    String S="1";
    for(int i=1;i<=n;i++)
        S+="0";
    S+="1";
    return S;
}

}

  • created

    Sep '21
  • last reply

    Oct '21
  • 1

    reply

  • 587

    views

  • 2

    users

  • 1

    link

21 days later

Could those parseLongs ever overflow? I couldn’t find a test case to make it happen, but that’s all I can think of.