I have used the following logic(in java) but the solution gives a runtime error.

long a[]=new long[n+1];

a[1]=0;a[2]=1;a[3]=1;

int i;

for(i=4;i<=n;i++)

{

a[i]=a[i-2]+a[i-3];

}

System.out.println(a[n]%1000000007);

I have given conditions to ensure nothing goes out of bounds. When I ran my programme in my IDE with a large input for *n* it gave a heap space error. How can I solve this?