Skip to content

Commit 2c1ec6c

Browse files
committed
fix: reject negative input in SumOfSquares
1 parent 8a20fa9 commit 2c1ec6c

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ private SumOfSquares() {
1818
*
1919
* @param n the target number
2020
* @return minimum number of squares needed
21+
* @throws IllegalArgumentException if {@code n} is negative
2122
*/
2223
public static int minSquares(int n) {
24+
if (n < 0) {
25+
throw new IllegalArgumentException("Input must be non-negative");
26+
}
27+
2328
if (isPerfectSquare(n)) {
2429
return 1;
2530
}

0 commit comments

Comments
 (0)