Files
Java/src/main/java/com/thealgorithms/others/TwoPointers.java
Oleksandr Klymenko ef93cc1503 refactor: TwoPointers (#6374)
* refactor: TwoPointers

* refactor: fix test formatting

* refactor: fix checkstyle

* refactor: fix checkstyle
2025-07-13 09:25:26 +00:00

46 lines
1.2 KiB
Java

package com.thealgorithms.others;
/**
* The two-pointer technique is a useful tool to utilize when searching for
* pairs in a sorted array.
*
* <p>
* Link: https://www.geeksforgeeks.org/two-pointers-technique/
*/
public final class TwoPointers {
private TwoPointers() {
}
/**
* Checks whether there exists a pair of elements in a sorted array whose sum equals the specified key.
*
* @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) {
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];
if (sum == key) {
return true;
}
if (sum < key) {
left++;
} else {
right--;
}
}
return false;
}
}