Question: I need to write a java programming method that uses a divide-and-conquer strategy to find the maximum value in an integer array. The maximum value
I need to write a java programming method that uses a divide-and-conquer strategy to find the maximum value in an integer array. The maximum value of a one-element array is that element. The maximum of any other array is the maximum of the left half, or the maximum of the right half, whichever is larger. (You can use ArraySum as a starting point.)
And ArraySum is:
public class ArraySum { public static void main(String[] args) { int[] test = {3, 4, 5, 1, 2, 3, 2}; // sum should be 20 int result = arraySum(test); System.out.println(result); } public static int arraySum(int[] arr) { return arraySumRec(arr, 0, arr.length - 1); } private static int arraySumRec(int[] arr, int start, int end) { if (start == end) { return arr[start]; } else { int mid = (start + end) / 2; int leftSum = arraySumRec(arr, start, mid); int rightSum = arraySumRec(arr, mid + 1, end); return leftSum + rightSum; } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
