From e888f4638c2af200caf0e3d633aaadf0acaf0fbb Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 12 Jul 2025 22:40:44 +0200 Subject: [PATCH 1/4] refactor: TwoPointers --- .../com/thealgorithms/others/TwoPointers.java | 33 +++++++++++-------- .../thealgorithms/others/TwoPointersTest.java | 10 ++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/TwoPointers.java b/src/main/java/com/thealgorithms/others/TwoPointers.java index c551408c38b9..c87e26269386 100644 --- a/src/main/java/com/thealgorithms/others/TwoPointers.java +++ b/src/main/java/com/thealgorithms/others/TwoPointers.java @@ -7,30 +7,37 @@ *

* Link: https://www.geeksforgeeks.org/two-pointers-technique/ */ -final class TwoPointers { +public final class TwoPointers { + private TwoPointers() { } /** - * Given a sorted array arr (sorted in ascending order), find if there exists - * any pair of elements such that their sum is equal to the key. + * Checks whether there exists a pair of elements in a sorted array whose sum equals the specified key. * - * @param arr the array containing elements (must be sorted in ascending order) - * @param key the number to search - * @return {@code true} if there exists a pair of elements, {@code false} otherwise. + * @param arr a sorted array of integers in ascending order (must not be null) + * @param key the target sum to find + * @return {@code true} if there exists at least one pair whose sum equals {@code key}, {@code false} otherwise + * @throws IllegalArgumentException if {@code arr} is {@code null} */ public static boolean isPairedSum(int[] arr, int key) { - int i = 0; // index of the first element - int j = arr.length - 1; // index of the last element + if (arr == null) { + throw new IllegalArgumentException("Input array must not be null."); + } + + int left = 0; + int right = arr.length - 1; + + while (left < right) { + int sum = arr[left] + arr[right]; - while (i < j) { - int sum = arr[i] + arr[j]; if (sum == key) { return true; - } else if (sum < key) { - i++; + } + if (sum < key) { + left++; } else { - j--; + right--; } } return false; diff --git a/src/test/java/com/thealgorithms/others/TwoPointersTest.java b/src/test/java/com/thealgorithms/others/TwoPointersTest.java index 3a174e0cd19e..ed3005787fdf 100644 --- a/src/test/java/com/thealgorithms/others/TwoPointersTest.java +++ b/src/test/java/com/thealgorithms/others/TwoPointersTest.java @@ -1,6 +1,8 @@ package com.thealgorithms.others; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import org.junit.jupiter.api.Test; @@ -69,4 +71,12 @@ void testPairExistsAtEdges() { int key = 9; assertTrue(TwoPointers.isPairedSum(arr, key)); } + + @Test + void isPairedSum_shouldThrowException_whenArrayIsNull() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { + TwoPointers.isPairedSum(null, 10); + }); + assertEquals("Input array must not be null.", exception.getMessage()); + } } From 62380daeb0ea1634bd6531332b5f1bc7a4b33979 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 12 Jul 2025 22:43:47 +0200 Subject: [PATCH 2/4] refactor: fix test formatting --- src/test/java/com/thealgorithms/others/TwoPointersTest.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/TwoPointersTest.java b/src/test/java/com/thealgorithms/others/TwoPointersTest.java index ed3005787fdf..7beca4d4bfec 100644 --- a/src/test/java/com/thealgorithms/others/TwoPointersTest.java +++ b/src/test/java/com/thealgorithms/others/TwoPointersTest.java @@ -74,9 +74,7 @@ void testPairExistsAtEdges() { @Test void isPairedSum_shouldThrowException_whenArrayIsNull() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> { - TwoPointers.isPairedSum(null, 10); - }); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {TwoPointers.isPairedSum(null, 10);}); assertEquals("Input array must not be null.", exception.getMessage()); } } From 03a705ffffdc95b83dcc804b03f08c0fa9e9b5fc Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 12 Jul 2025 22:45:19 +0200 Subject: [PATCH 3/4] refactor: fix checkstyle --- src/test/java/com/thealgorithms/others/TwoPointersTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/TwoPointersTest.java b/src/test/java/com/thealgorithms/others/TwoPointersTest.java index 7beca4d4bfec..9a35b2eeccef 100644 --- a/src/test/java/com/thealgorithms/others/TwoPointersTest.java +++ b/src/test/java/com/thealgorithms/others/TwoPointersTest.java @@ -74,7 +74,7 @@ void testPairExistsAtEdges() { @Test void isPairedSum_shouldThrowException_whenArrayIsNull() { - IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {TwoPointers.isPairedSum(null, 10);}); + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TwoPointers.isPairedSum(null, 10)); assertEquals("Input array must not be null.", exception.getMessage()); } } From 75a77415cf069b48c8b6ad155e92f4d21d983a66 Mon Sep 17 00:00:00 2001 From: alxkm Date: Sat, 12 Jul 2025 22:48:10 +0200 Subject: [PATCH 4/4] refactor: fix checkstyle --- src/test/java/com/thealgorithms/others/TwoPointersTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/TwoPointersTest.java b/src/test/java/com/thealgorithms/others/TwoPointersTest.java index 9a35b2eeccef..6e0d2b22d280 100644 --- a/src/test/java/com/thealgorithms/others/TwoPointersTest.java +++ b/src/test/java/com/thealgorithms/others/TwoPointersTest.java @@ -73,7 +73,7 @@ void testPairExistsAtEdges() { } @Test - void isPairedSum_shouldThrowException_whenArrayIsNull() { + void isPairedSumShouldThrowExceptionWhenArrayIsNull() { IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TwoPointers.isPairedSum(null, 10)); assertEquals("Input array must not be null.", exception.getMessage()); }