mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
* refactor: TwoPointers * refactor: fix test formatting * refactor: fix checkstyle * refactor: fix checkstyle
46 lines
1.2 KiB
Java
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;
|
|
}
|
|
}
|