• Post Reply Bookmark Topic Watch Topic
  • New Topic

Recursive backtracking help  RSS feed

 
Manoj Prabahar Raju
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All. I'm new to Java. Could you please help me with understand a codingbat exercise. This is recursive backtracking.

groupSum(0,{4,2,8},10)--> true


In the above code after first check of if (start >= nums.length) return (target == 0); how the control passes to if (groupSum(start + 1, nums, target)) return true; instead of if (groupSum(start + 1, nums, target - nums[start])) return true;

May be this is simple. Please help. I have been trying to debug the code for past two days and trying to get the concept but not able to. Thank you!
 
Manoj Prabahar Raju
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone please reply!!?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recursion is difficult to debug. You could write down in paper what is going then it only you can figured it out..


I have written some here.

 
Manoj Prabahar Raju
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tushar

Thank you so much for the reply. I have the below doubt.

I can get the False will be returned in first IF statement when comparing targets(0==-4). Does this false makes the below statement as False?



Also what does the return true does in the above statement?

Sorry if all I ask is too simple. Just want to learn it correct.
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Prabahar Raju wrote:Anyone please reply!!?

Please read this.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Prabahar Raju wrote:
I can get the False will be returned in first IF statement when comparing targets(0==-4). Does this false makes the below statement as False?




First, I don't know what you mean by "below statement"... it seems to imply that the next line of the method is going to execute if false is returned. This is simply *not* true, if either true or false is returned, the method is done -- and it will either return true or false. The only way the next line of the method is going to execute is if the condition fails, and the return statement is not executed.

Manoj Prabahar Raju wrote:
Also what does the return true does in the above statement?


Well, you probably need to understand the previous first ... but basically, it is unwinding the call stack. When the groupSum() method returns true, the calling groupSum() method will also return true, etc. until the whole stack unwinds, and the first called groupSum() method returns true.

Henry
 
Manoj Prabahar Raju
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Ritchie. I didn't know this earlier. I would be patient in future. Thank you for sharing the link.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!