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..6e0d2b22d280 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,10 @@ void testPairExistsAtEdges() { int key = 9; assertTrue(TwoPointers.isPairedSum(arr, key)); } + + @Test + void isPairedSumShouldThrowExceptionWhenArrayIsNull() { + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TwoPointers.isPairedSum(null, 10)); + assertEquals("Input array must not be null.", exception.getMessage()); + } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy