There was a similar coding question at hacker earth. The challenge ends now. I don't have that question now but my code looked like this. I passed the test cases but my code was not accepted due to large input gives this error. I have written the following code.
import java.util.*;
class TripleStep{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
int n=10000;
long[] memo=new long[n+1];
Arrays.fill(memo,-1);
long ans=StairOpr(n,memo);
Arrays.fill(memo,-1);
System.out.println(ans);
}
public static long StairOpr(int n,long[] memo){
if(n<0){
return 0;
}
else if(n==0){
return 1;
}
else if(n==1){
return 2;
}
else if(n==2){
return 3;
}
else if(memo[n]>-1){
return memo[n];
}
else
memo[n]= (StairOpr(n-1,memo)%1000000007+StairOpr(n-2,memo)%1000000007+StairOpr(n-3,memo)%1000000007+n*n*(n+1)%1000000007)%1000000007;
return memo[n];
}
}
The code gives me the following error for the input value 10000.
Exception in thread "main" java.lang.StackOverflowError
at TripleStep.StairOpr(TripleStep.java:31)
at TripleStep.StairOpr(TripleStep.java:31)
What is the reason and what would be the solution? Since I write dynamic programming code but due to error the answer is not Submitted and I face the rejection.