Skip to content

refactor: TwoPointers #6374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions src/main/java/com/thealgorithms/others/TwoPointers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,37 @@
* <p>
* 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;
Expand Down
8 changes: 8 additions & 0 deletions src/test/java/com/thealgorithms/others/TwoPointersTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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());
}
}
Loading