From 413318463923c00b4c6b70eb3e4280141f669223 Mon Sep 17 00:00:00 2001 From: Swati Vusurumarthi Date: Fri, 6 Feb 2026 02:54:40 +0530 Subject: [PATCH] ### 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 --- .../com/thealgorithms/backtracking/ArrayCombination.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java index f8cd0c40c20e..d05e33a4242f 100644 --- a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java +++ b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java @@ -20,8 +20,8 @@ private ArrayCombination() { * @throws IllegalArgumentException if n or k are negative, or if k is greater than n. */ public static List> combination(int n, int k) { - if (n < 0 || k < 0 || k > n) { - throw new IllegalArgumentException("Invalid input: n must be non-negative, k must be non-negative and less than or equal to n."); + if (k < 0 || k > n) { + throw new IllegalArgumentException("Invalid input: 0 ≤ k ≤ n is required."); } List> combinations = new ArrayList<>(); @@ -48,7 +48,7 @@ private static void combine(List> combinations, List curr for (int i = start; i < n; i++) { current.add(i); combine(combinations, current, i + 1, n, k); - current.remove(current.size() - 1); // Backtrack + current.removeLast(); // Backtrack } } }