Stack Overflow in java for recursive function and the same code runs properly in c++

Revision en1, by rahulkhairwar, 2015-11-08 00:30:35

I was solving this question on dp and I use Java as my preferred language for programming.

Here is the Java submission : link
and here is the C++ submission : link
But, I got a stack overflow error when I submitted the solution, while the same code when converted into C++, ran perfectly fine. So, I searched on the internet and found out that Java's stack space is very less. :/
So, is there any way I could solve this question recursively in Java? I don't want to switch to C++ as I am very comfortable with Java and really like to code in it.

Java users, do you always implement an iterative version of an algorithm which could be solved recursively?

Tags java, recursion, c++, stack overflow

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English rahulkhairwar 2015-11-08 00:32:00 270 Tiny change: 'e solution, while th' -> 'e solution in Java, while th'
en1 English rahulkhairwar 2015-11-08 00:30:35 1061 Initial revision (published)