Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

hi i neeeed hellpp with this homework assignment that i need to complete a metho

ID: 3685328 • Letter: H

Question

hi i neeeed hellpp with this homework assignment that i need to complete a method that i need to complete but i dont know how to complete it

/**
* Find the largest element in an array of integers
*
* @param a Array of integers
* @return largest value
*/
public static int findMax(int[] a) {
return findMax(a, 0);
}

/**
* Find the largest element in an array of integers
*
* @param a Array of integers
* @param i Starting index
* @return largest value
*/
private static int findMax(int[] a, int i) {
if (i < a.length) {
return Math.max(a[i], findMax(a, i + 1));
} else {
return Integer.MIN_VALUE;
}
}

I need help to do his it is suppose to be the same except by cutting the array in half and searching each half for the largest value recursively.

The header for such a method may as follows:

/**
* Find the largest element in an array of integers
* by cutting the array in half. Still O(n) but reduces
* the runtime stack log(n)
* @param data Array of integers
* @param a Starting index
* @param b Ending index
* @return
*/
int findMax(int[] data, int a, int b) {

}

Explanation / Answer

int findMax(int[] data, int a, int b) {
       if(a!=b)
           return Math.max(findMax(data, a, (a+b)/2),findMax(data, (a+b)/2+1, b));
       else
           return data[a];
}