Longest Collatz sequence using dynamic programming in Java 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 &gt; 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 &gt;= 1){
                    count = 0;
                    if(countarray[num-1] != 0)
                        count = countarray[num-1];
                    else
                        counter(num);
                    if(count &gt; max){
                        max = count;
                        result = num;
                    }
                    num--;
            }
            t--;
            System.out.println(result);    
        }

    }
    public static int counter(int temp){
        if(temp &lt;= 1)
            return count;
        if(temp &lt; 5000000 &amp;&amp; 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 &lt; 5000000){
            countarray[temp-1] = count;
        }
        return count;
    }
}
You memoized once, but you needed to do it twice

Suppose you started with the entire countarray filled in for free.  Your program would still time out because you have \$10^4\$ test cases, and for each test case, you are searching up to \$5*10^6\$ array entries for a total of \$5*10^{10}\$ operations.  In other words, your program runs in \$O(T*N)\$ time, which is too slow.

Think about what happens when the first test case is 5000000 and the second test case is 4999999.  When you found the answer for the first test case, you should have also found the answer for the second test case.  But you 
didn't memoize that anywhere.  If you just did a pass where you found the answer for each n once and saved the answers in a second memoization array, then each test case would take \$O(1)\$ time.  Then your total time would be \$O(T + N)\$, which is \$O(N)\$ to build the answer array and \$O(T)\$ to handle \$T\$ test cases.

Be careful of overflow

When computing the collatz sequence, it is easy to overflow a 32-bit integer.  I would recommend using a long instead of an int.  You might be overflowing and not knowing it because the if (temp &lt;= 1) check may be cutting the sequence short.