Remove array len + Math.max (#4401)

This commit is contained in:
Lukas 2023-09-25 21:10:04 +02:00 committed by GitHub
parent 01157f299c
commit cada67b1eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,7 +17,7 @@ public class FindMaxRecursion {
array[i] = rand.nextInt() % 100;
}
assert max(array, array.length) == Arrays.stream(array).max().getAsInt();
assert max(array) == Arrays.stream(array).max().getAsInt();
assert max(array, 0, array.length - 1) == Arrays.stream(array).max().getAsInt();
}
@ -39,17 +39,16 @@ public class FindMaxRecursion {
int leftMax = max(array, low, mid); // get max in [low, mid]
int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]
return Math.max(leftMax, rightMax);
return leftMax < rightMax ? rightMax : leftMax;
}
/**
* Get max of array using recursion algorithm
*
* @param array contains elements
* @param len length of given array
* @return max value of {@code array}
*/
public static int max(int[] array, int len) {
return len == 1 ? array[0] : Math.max(max(array, len - 1), array[len - 1]);
public static int max(int[] array) {
return array.length == 1 ? array[0] : max(array, 0, array.length);
}
}