What is wrong in this code , it is running in logN time , then why it is showing run time error

public static void main(String[] args) {
	
	Scanner sc=new Scanner(System.in);
	int n=sc.nextInt();
	
	System.out.println(exchange(n , new int[n+1]));
}


static int exchange(int n , int[] arr)
{
	if(n==0) return 0;
	if(n==1) return 1;
	
	if(arr[n]!=0) return arr[n];
	
	int max = exchange(n/2 , arr) + exchange(n/3 , arr) + exchange(n/4 , arr);

	max=Math.max(max, n);

	arr[n]=max;
	return max;

// int[] storage = new int[n+1];
// storage[1]=1;
//
// for(int j=2 ; j<=n ; j++)
// {
// storage[j] = Math.max( j , storage[j/2] + storage[j/3] + storage[j/4] );
// }
//
// return storage[n];

}

@Himanshu-Jhawar-2273952536067590 bro the error is not tle, basically you can’t make array that big as given in constraints, so instead you can use hashmap as memo, I have corrected your code below!

If your doubt is cleared do mark it resolved!
Happy coding!

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int n=sc.nextInt();

System.out.println(exchange(n , new HashMap<Integer, Long>()));

}

static long exchange(int n , Map<Integer, Long> memo)

{

if(n==0) return 0;

if(n==1) return 1;

if(memo.containsKey(n)) return memo.get(n);

long max = exchange(n/2 , memo) + exchange(n/3 , memo) + exchange(n/4 , memo);

max=Math.max(max, n);

memo.put(n, max);

return max;

// int[] storage = new int[n+1];

// storage[1]=1;

//

// for(int j=2 ; j<=n ; j++)

// {

// storage[j] = Math.max( j , storage[j/2] + storage[j/3] + storage[j/4] );

// }

//

// return storage[n];

}

}

thanks for clearing doubt