Skip to content

Commit 67f743a

Browse files
Refactor FindMax class and improve findMax method
Refactored the FindMax class by simplifying the constructor and optimizing the findMax method's checks and loop.
1 parent fc3fdaf commit 67f743a

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

src/main/java/com/thealgorithms/maths/FindMax.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
package com.thealgorithms.maths;
22

33
public final class FindMax {
4-
private FindMax() {
5-
}
4+
private FindMax() {}
65

7-
/**
8-
* @brief finds the maximum value stored in the input array
9-
*
10-
* @param array the input array
11-
* @exception IllegalArgumentException input array is empty
12-
* @return the maximum value stored in the input array
13-
*/
146
public static int findMax(final int[] array) {
15-
int n = array.length;
16-
if (n == 0) {
7+
if (array.length == 0) {
178
throw new IllegalArgumentException("Array must be non-empty.");
189
}
10+
1911
int max = array[0];
20-
for (int i = 1; i < n; i++) {
21-
max = Math.max(array[i] , max);
12+
for (int i = 1; i < array.length; i++) {
13+
max = Math.max(array[i], max);
2214
}
2315
return max;
2416
}

0 commit comments

Comments
 (0)