• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Recursive backtracking help

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone please reply!!?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Manoj Prabahar Raju wrote:Anyone please reply!!?


Please read this.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Can you really tell me that we aren't dealing with suspicious baked goods? And then there is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic