https://www.spoj.com/problems/ADDREV/
My answer to ADDREV challenge on SPOJ (link to the problem given above), written in JAVA, was accepted. However, the timing was 1.23 and memory used was 82M, which doesn't seem impressive/up-to-the-mark.
I'm new to competitive programming and want to know -
a. In what ways can I optimize my code to take up lesser memory and be quicker?
b. Are their any incorrect programming practices that I have unknowingly followed?
c. Which better way is available to solve this problem in Java?
My code:-
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner obj = new Scanner(System.in);
int t = obj.nextInt(); obj.nextLine(); //clears the buffer
for(int i=1;i<=t;++i)
{
Scanner obj1 = new Scanner(obj.nextLine());
StringBuffer b1 = new StringBuffer(obj1.next());
StringBuffer b2 = new StringBuffer(obj1.next());
b1.reverse(); b2.reverse();
BigInteger i1 = new BigInteger(b1+"");
BigInteger i2 = new BigInteger(b2+"");
i1 = i1.add(i2);
b1 = new StringBuffer(i1.toString());
b1.reverse();
String a = new String(b1.toString());
for(int j=0;;j++) //To remove leading zeroes (if any)
{
if(a.charAt(j)!='0')
{
System.out.println(a.substring(j));
break;
}
}
}
}
}