I'm not getting where else should I optimize it. Is there any way to optimize this code any further? Just give me hints.
import java.io.*;
import java.util.*;
public class Solution {
static int[] countarray = new int[5000000]; //for memoization
static int count; //for calculating length
public static void main(String[] args){
countarray[0]=0;
Scanner scan = new Scanner(System.in);
int t = scan.nextInt(); //number of inputs
while(t > 0){
int num = scan.nextInt(); // number upto which we have to find maximum length of the sequence
int max = 0;
int result = 1;
while(num >= 1){
count = 0;
if(countarray[num-1] != 0)
count = countarray[num-1];
else
counter(num);
if(count > max){
max = count;
result = num;
}
num--;
}
t--;
System.out.println(result);
}
}
public static int counter(int temp){
if(temp <= 1)
return count;
if(temp < 5000000 && countarray[temp-1] != 0){
count += countarray[temp-1];
return count;
}
if(temp % 2 == 0)
counter(temp/2);
else
counter((3 * temp) + 1);
count++;
if(temp < 5000000){
countarray[temp-1] = count;
}
return count;
}
}