0
public static int counter = 0 ;
public static int max = 0 ;

public static final int findMaxRecursively(List<Integer> numbers) {
   if (numbers.get(counter) > max){
        max = numbers.get(counter);
        counter++;
   }

   if (counter == numbers.size() - 1){
        return max;
   }

  counter++;
  return findMaxRecursively(numbers);
}

I have an assignment that asks me to find the largest number in the list numbers using recursion.

The above code is throwing an index exception which I believe is being caught in main which I do not have access to.

What is wrong with my code / logic?

EDIT

Thanks for the replies.

I went ahead and removed the first counter, I understand what I broke there, but that still does not allow me to find the max number.

Here's the assignment:

/*
 * findMaxRecursively
 *
 * Takes a list of numbers and finds the largest among them
 * using recursive calls.
 *
 * @param numbers a list of numbers, can be odd or even numbered
 * @return the largest number in the list
 *
 * Hint: your base case may be a comparison of 2 numbers
 */

Am I correctly executing recursion by doing :

return finMaxRecursively(numbers):
stamps
  • 211
  • 1
  • 4
  • 11
  • This is all wrong, if you are going to use recursion, do NOT use static methods in it, everything you need pass by parameters. Also if you find max number, you increment counter and then you increment it again. – libik Mar 28 '15 at 20:17
  • @libik I believe you mean not use or rely on static variables. The method itself can be static and logically here it should be as it should not depend on anything but the list passed to it. –  Mar 28 '15 at 20:23
  • @A.J. - yea, I meant variables, not methods, tx :). – libik Mar 28 '15 at 20:29
  • @libik how can I not use static variables if the method is static? I did not create the method. – stamps Mar 28 '15 at 20:45

2 Answers2

1

Let's say counter is size - 2 before the end of the method. counter++ makes it size - 1. Then at the start of the next call, you find that the index of size - 1 has the largest number. So, you set that to max and call count++ again.
Now count is equal to size, so the if case doesn't catch it. The next time around you will be attempting to access and index that is not allowed (>= numbers.size()) I would suggest removing the first counter++ as it is not needed.

0

If you want to find max value present in array using recursion use it as below.

public static int findMax(int[] a, int index){
    if (index > 0) {
        return Math.max(a[index], findMax(a, index-1))
    } 

   else {
        return a[0];
    }
}

Specifically for ArrayList you can use below method

public static final int findMaxRecursively(List<Integer> numbers) 
{
     if(numbers.size() == 1)
         return numbers.get(0);

     int bottom = findMaxRecursively(numbers.subList(0,numbers.size()/2));

     int top = findMaxRecursively(numbers.subList(numbers.size()/2,numbers.size()));

     return top>bottom?top:bottom;
}

Also take a look into Find maximum value in an array by recursion

Community
  • 1
  • 1
Om.
  • 2,532
  • 4
  • 22
  • 22