Jump to content

Need help with a recursion problem on CodingBat

- - - - -

  • Please log in to reply
2 replies to this topic

#1
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
The problem link is this:

CodingBat Java Recursion-2 groupSum5

Now, I've never done this kind of recursion problem before, the kind that has more than 1 argument in the method. Just need a complete explanation on how to go about this. Any help is greatly appreciated.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Giving that this is a public challenge, part of the point is to try to solve it yourself.

In general, you want to think about how you can change each of the three arguments to indicate progress.

As an initial start, however, I'd simply look at the four sample calls and make sure you understand why each of them produces the results indicated, given the problem description.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
bloodchains

bloodchains

    Learning Programmer

  • Members
  • PipPipPip
  • 73 posts
I guess I should've also posted what I've done so far so people don't think I'm just asking for the answer straight up. I know what I'm supposed to do and how that problem works, but I don't know how I'm supposed to do the recursion part. I'll post what I have so far later, when I get home.

EDIT: Here's what I have so far:

public boolean groupSum5(int start, int[] nums, int target) {
  int c = 0;
  int sum = 0;
  int start1 = nums[0];
  int target1 = nums[nums.length  - 1];
  
  if (start > 0)
     return false;
  else if (nums[c] % 5 == 0)
  {
     sum += nums[c];
     c++;
  }  
  groupSum5(start1, nums, target1);
}

I know I didn't type in the return value yet. I just need help figuring out how to do the recursion so that it will reach termination instead of recurring infinitely.

Edited by bloodchains, 22 April 2011 - 07:14 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users