Exchaanging Coins

test case 2 is not passing , please tell me the error in my program https://ide.codingblocks.com/s/201548
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner obj = new Scanner(System.in);
int a = obj.nextInt();

int arr[] = new int[a+1];
for(int i=0;i<=a;i++){
arr[i] = -1;
}
System.out.println(coins(arr,a));

}

public static int coins(int[]arr, int n)
{if( arr[n] != -1)
	return arr[n];

	if(n<12)
	{arr[n]=n;
	return arr[n];}

	int find = Math.max(n,coins(arr,n/2)+coins(arr,n/3)+coins(arr,n/4));
	arr[n]=find;
	return arr[n];
}

}

is it showing TLE, or some space error? can you please mention the error clearly, since your code looks fine to me.
thanks

sir ,
it is showing WRONG ANSWER for test case 2.

hi, this problem makes you learn about space limits. if you make an integer array of say input 10^9, you are consuming approx 4GB memory, which might not be permissible. so you need to make a balance between time and space complexity.
the same problem can be accessed in https://www.hackerearth.com/practice/algorithms/dynamic-programming/state-space-reduction/practice-problems/algorithm/bytelandian-gold-coins/description/

here is the solution(wrt hackerearth problem):

/* IMPORTANT: Multiple classes and nested static classes are supported */

// * uncomment this if you want to read input.
//imports for BufferedReader
import java.io.BufferedReader;
import java.io.InputStreamReader;

//import for Scanner and other utility classes
import java.util.*;

// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail

class TestClass {
public static void main(String args[] ) throws Exception {
// Sample code to perform I/O:
//Use either of these methods for input

    //BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String name;
    try{
        while((name = br.readLine())!=null){
            //Scanner obj = new Scanner(System.in);
            int a = Integer.parseInt(name);

            long arr[] = new long[10000000];
            for(int i=0;i<10000000;i++){
                arr[i] = -1;
            }
        System.out.println(coins(arr,a));
        }
    }catch(Exception ex){}             }  
}

public static long coins(long[]arr, int n){
    if( n<10000000 && arr[n]!=-1)
        return arr[n];

    if(n<12){
        arr[n]=n;
        return arr[n];}

    long find = Math.max(n,coins(arr,n/4)+coins(arr,n/3)+coins(arr,n/2));
    if(n<10000000) arr[n]=find;
    return find;
}

}

have a look and try to understand the space limits.
thanks

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.