You are not logged in. Please login at www.codechef.com to post your questions!

×

# JAVA - Stack Size - Recursion Limit

 1 1 There are many problems requiring deep recursion. Usually it's graph problems with big limitations which are solved by DFS (or solving recurrence of Dynamic Programming recursively). But deep recursion requires large stack size, often several megabytes. The default Java stack size is quite small. Most of the times it would give Runtime error on Codechef, SPOJ. But when I see the solutions of other user in C++ they don't have such issue. It results in writing non-recursive DFS which is sad itself. Is there any way to run these recursive codes in Java without NZEC errors? asked 24 Dec '15, 11:19 415●1●14 accept rate: 8%

 4 Actually there is a known way to set stack size in java. Do it like this:  new Thread(null, new Runnable() { public void run() { new Main().run(); } }, "1", 1 << 23).start();  See this post http://codeforces.com/blog/entry/166 answered 10 Sep '16, 01:36 46 accept rate: 100%
 0 You cannot explicitly increase the stack size. But if you could somehow manage to covert your code to end tail recursion, you are done because in end tail recursion stacking of elements doesn't take place. This is my aced iterative solution: http://pastebin.com/VL8zN7ta answered 24 Dec '15, 14:44 1★arpit728 693●19●68 accept rate: 10% I am explicitly not looking for DFS. I know iterative version of DFS. My question is there any way to run those recursion codes in Java without getting NZEC errors. I mean recursive codes like in C++ not changing it to iteration. (24 Dec '15, 15:51) @ankurverma1994 can you share that c++ code. (24 Dec '15, 21:19) arpit7281★ @arpit728 I don't think you are getting my point. I am saying the recursive solution in Java gives NZEC whereas same code gets AC in C++. DFS is an example of such recursive codes. You can look at some codes Java NZEC(recursive code) Java AC(iterative) C++ AC (recursive) (24 Dec '15, 22:23) Similarly, there are many such examples like dynamic programming problems where recursive solution results to NZEC error. So I have to write iterative version of it. But when I see solution of users in C++ there recursive solution gets AC. (24 Dec '15, 22:28)
 0 onepibin paviox Paket Wisata Pulau Tidung ebetic azurub onoruf eketuk amupug exepuf ijureh isutuh oqotih usutek utinej uparif answered 24 Dec '15, 15:50 0★jones13 1 accept rate: 0%
 0 Adding to other answers... The maximum heap size can be determined by running the following command on the corresponding platform: Windows: java -XX:+PrintFlagsFinal -version | findstr HeapSize Linux/MacOS: java -XX:+PrintFlagsFinal -version | grep HeapSize answered 02 Oct '16, 17:15 3★smsubham 675●2●16 accept rate: 15%
 toggle preview community wiki:
Preview

### Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×1,314
×435
×21

question asked: 24 Dec '15, 11:19

question was seen: 3,441 times

last updated: 02 Oct '16, 17:15