4 / 4
Feb 2019

I have opened an account for Ridit, one of 7-years-old students learning Java. The first task i gave to him was PALIN -The Next Palindrome. After i explained it to him, he was able to solve it mostly except removing the leading zeros, which i did. Following is his solution of the problem -

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	try {
		Scanner in = new Scanner(System.in);
		int t = Integer.parseInt(in.nextLine());
		String[] numbersInString = new String[t];
		
		for (int i = 0; i <t; i++) {
			String str = in.nextLine();
			numbersInString[i] = removeLeadingZeros(str);
		}
		
		for (int i = 0 ; i<t; i++) {
			int K = Integer.parseInt(numbersInString[i]);
			int answer = findTheNextPalindrome(K);
			System.out.println(answer);
			
		}
	}catch(Exception e) {
		return;
	}
}
static boolean isPalindrome(int x) {
	String str = Integer.toString(x);
	int length = str.length();
	StringBuffer strBuff = new StringBuffer();
	for(int i = length - 1;i>=0;i--) {
		char ch = str.charAt(i);
		strBuff.append(ch);
	}
	String str1 = strBuff.toString();
	if(str.equals(str1)) {
		return true;
	}
	return false;	
}
static int findTheNextPalindrome(int K) {
	for(int i = K+1;i<9999999; i++) {
		if(isPalindrome(i) == true) {
			return i;
		}
	}
	return -1;
}
static String removeLeadingZeros(String str) {
	String retString = str;
	if(str.charAt(0) != '0') {
		return retString;
	}
	return removeLeadingZeros(str.substring(1));
}

}

It is giving correct answer in Eclipse on his computer, but it is failing in SPOJ. If someone helps this little boy in his first submission, it will definitely make him very happy. I am not sure if i should post the code on this forum. Hence if this is not the right thing, i will remove this post. Thank you in advance…

  • created

    Feb '19
  • last reply

    Feb '19
  • 3

    replies

  • 1.6k

    views

  • 2

    users

I didn’t understand that this problem requires some Optimization. Ridit is just 7 years. Hence he is too young to know this side of the programming. i have asked him to stop it here and have started giving him problems from Basics SPOJ. Actually it was my mistake to assign this problem to a kid without doing the proper research. Anyway thank you for your time…

  • Ridit’s CS teacher…

If you show the “Ranking” page for a problem, the number of submissions vs the number of accepted, wrong answer, runtime error and time limit exceeded, will give you an idea of how hard a problem is.

You could also show the Problems list, and sort on the number of accepted users - again, that’ll give you an idea of the easier problems.

FWIW, I was actually quite impressed with the neatness and layout of the code, to the extent I doubted it was really created by a seven year old.