Skip to content

Commit 3835c48

Browse files
swativ15swativs15
andauthored
Refactor: simplify validation and improve backtracking cleanup (#7258)
### Summary This PR makes small readability and maintainability improvements to the algorithm implementation. ### Changes - Removed a redundant `n < 0` validation check since the method contract already ensures valid `n` - Replaced `current.remove(current.size() - 1)` with `current.removeLast()` to better express backtracking intent ### Rationale - Simplifies input validation without changing behavior - Uses the `Deque` API to make the backtracking step clearer and less error-prone ### Impact - No change in algorithm logic or time/space complexity - Output remains identical Co-authored-by: Swati Vusurumarthi <swativs869@gmail.com>
1 parent 249b88f commit 3835c48

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/main/java/com/thealgorithms/backtracking/ArrayCombination.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ private ArrayCombination() {
2020
* @throws IllegalArgumentException if n or k are negative, or if k is greater than n.
2121
*/
2222
public static List<List<Integer>> combination(int n, int k) {
23-
if (n < 0 || k < 0 || k > n) {
24-
throw new IllegalArgumentException("Invalid input: n must be non-negative, k must be non-negative and less than or equal to n.");
23+
if (k < 0 || k > n) {
24+
throw new IllegalArgumentException("Invalid input: 0 ≤ k ≤ n is required.");
2525
}
2626

2727
List<List<Integer>> combinations = new ArrayList<>();
@@ -48,7 +48,7 @@ private static void combine(List<List<Integer>> combinations, List<Integer> curr
4848
for (int i = start; i < n; i++) {
4949
current.add(i);
5050
combine(combinations, current, i + 1, n, k);
51-
current.remove(current.size() - 1); // Backtrack
51+
current.removeLast(); // Backtrack
5252
}
5353
}
5454
}

0 commit comments

Comments
 (0)