0

I have this so far, I just need it to start at any index value given by a parameter, how would I add this.

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

   else {
        return a[0];
    }
}
4
  • 1
    What's wrong with what you have? Commented Apr 26, 2015 at 22:38
  • 1
    You'll probably get a good answer since the problem/scenario is so simple, but in the future, try to add some context to your code segment, explain some variables, and show some research or testing effort on your part. Commented Apr 26, 2015 at 22:44
  • I don't think I was clear enough. My program works right now. But I want it to find the max, and start at the index value that the function is passed. For example if the Array is {9,4,3,5,6,} and the index value for it to start at is 2, the max value returned would be 6. Does this make sense? Commented Apr 26, 2015 at 22:58
  • Any hints on how I implement this? I've honestly been trying and just not sure how to do it recursively. Commented Apr 26, 2015 at 23:04

5 Answers 5

2

(not including code, as it is really trivial)

if you need to search for max element from index i to the end of array

1) use i as start index as @pbabcdefp instructed

2) in recursion check use array length, not zero

3) increment i in the recursion call

4) fall back to i-th element when reached end of array

that's it

Sign up to request clarification or add additional context in comments.

Comments

1
public static int maxElement(int[] a, int i) {
  if (i < a.length - 1) {
    return Math.max(a[i], maxElement(a, i+1));
  } else {
    return a[a.length - 1];
  }
}

Go forward, adjust boundaries and you're done.

Comments

1
public static int maxIndex(int i, int[] a){
   if(i == a.length - 1) return a.length - 1;
   int j = maxIndex(i+1, a);
   if(a[i] > a[j])
     return i;
   return j;
}

Use it by:

System.out.println(a[maxIndex(0, a)]);

3 Comments

Where does n come from?
@ChiefTwoPencils Should be a.length - 1
It's a correct maxIndex implementation but I think he wants maxElement (ok I see you could argue it is the same because you can just use it as you describe)
0
public static int maxElement(int[] array, int index){
    //check all elements from index
    if (index >= 0 && index < array.length - 1) {
        return Math.max(array[index], maxElement(array, index+1));
    }
    // return last element
    return array[array.length -1];
}

Comments

0

maxElement method:

public static int maxElement(int[] a, int i) throws IndexOutOfLenght{

    if (i > a.length-1 || i < 0)
        throw new IndexOutOfLenght();
    else{
        if (i == a.length-1)
            return a[i];
        else            
            return Math.max(a[i], maxElement(a, i+1));
    }
}

Main:

public static void main(String[] args) throws IndexOutOfLenght {
    int[] array = {1,2,3,4,5,6};
    System.out.println(maxElement(array,1));
}

Exception class:

public class IndexOutOfLenght extends Exception {
     IndexOutOfLenght() {
        super("IndexOutOfLenght ");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.