Recursion and arrays

• 02-05-2009, 05:36 AM
zlwilly
Recursion and arrays
My mind is slowly becoming more open to the concept of recursion, but any suggestion as to how to go about the following would be greatly appreciated.

I want to make a recursive method that is given an integer array and is able to tell me whether or not it is the same front to back. IE. If given a size 7 array with integers 1 3 5 5 3 1 then it will return true, else it will return false.

I just need some help nailing down the thought process behind the recursion. I've done simple ones such as counting the frequency of a single digit inside a a larger integer and returning the final number, but I'm at a loss as how to pull this one off..

Thanks.
• 02-05-2009, 05:45 AM
Fubarable
Which way would be best? check the outer two numbers first or the inner numbers?

Once you figure that out, then answer these questions:
• so you check the two numbers decided on above, now you have to check a subset -- how to get the subset?
• what should the method return?
• what are the ending conditions?
• 02-05-2009, 06:06 AM
Fubarable
Well, I guess you're either not interested in answering my questions, or you've walked away from the computer. Can't say that I didn't try. Good night and much luck.
• 02-05-2009, 06:15 AM
zlwilly
So sorry, I've been away for a bit. Thanks for getting my mind started. Doing the two outer numbers seems like it would be difficult as they are so far apart. I can't get my head around recursion that could check the two opposite ends of the 'spectrum', I guess. So I'll start thinking about how to check internally and moved outward.

If the method is going to return a true or false, then the base case would have to do the same, wouldn't it? Just with what should be the final possible thing that could happen..

Any other suggestion would be great, but I think I'm going to tackle this in the morning, I've been maxxed out for the night, it's a little late here. :] Thanks so much!
• 02-05-2009, 06:22 AM
Fubarable
Quote:

Doing the two outer numbers seems like it would be difficult as they are so far apart.
Distance doesn't mean much here. Which numbers are easier to get a handle on, the outer items which have indices 0 and length - 1, or the inner which have indices, hmmm, let me see, is it an even or odd array, let me think this out algebraically,....

In this light, which numbers are easier to retrieve? Ignoring "distance" of course ;)

Quote:

I can't get my head around recursion that could check the two opposite ends of the 'spectrum', I guess. So I'll start thinking about how to check internally and moved outward.
think again.

Next think of the several ending conditions -- what to do with arrays of 0, 1, 2 items?

Next look into the Arrays class for a useful method that will help you recurse.
• 02-05-2009, 09:48 PM
zlwilly
I was able to get it working! Thanks for the suggestions. Here's the code in case anyone has need for it.

Code:

```      public static boolean forwardsEqualsBackwards( int[] n, int start, int end) {           if (n[start]!=n[end])               return (false);           else if (start==end)               return (true);           else if (end==start+1)               return (n[end]==n[start+1]);           else                return (forwardsEqualsBackwards(n, start+1, end-1));     }```
• 02-06-2009, 06:26 AM
Fubarable
congrats! You'll go far.