java:Does recursive function posses some problem..??

I was solving problem Garden Game of july14 long contest…and was getting runtime(NZEC) error because of one recursive function pcs(int x)…link .but got accepted when used iterative code of same recursive function…link to AC…So, my question is – Does recursive methods in java create some problem…?? Should we avoid using recursive methods in java…?.. Any help or suggestions would be appreciated…Thanks.

@yoyo123 : Yes in Java the program will soon run out of stack space and throw run time error if recursion is actually happening several times ( may be 1000 calls or more ) .

One solution is to start the recursion in a different thread . That gives it more stack space .

However writing an iterative version is always the more scalable approach in Java .

1 Like

@vineetpaliwal thanks for the answer…

//