Hi.. I am trying to solve the Bytelandian Gold Coins Problem and constantly getting NZEC error.. My code works fine when i run it on my computer.. I have even tried the boundary cases (i.e. taking large inputs). Here is my code
import java.io.*;
class COINS{
static long arr[] = new long[1000000];
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
COINS obj = new COINS();
String str = br.readLine();
int n;
while(str.length()!=0){
n=Integer.parseInt(str);
System.out.println(obj.coins(n));
str = br.readLine();
}
}
long coins(int n){
if(n<12)
return n;
if(n<1000000){
if(arr[n]!=0)
return arr[n];
arr[n] = coins(n/2)+coins(n/3)+coins(n/4);
return arr[n];
}
return coins(n/2)+coins(n/3)+coins(n/4);
}
}
I have tried making the array size smaller.. But it gives NZEC even for array size 10000.. Where am i going wrong? Please help!!