Skip to content

refactor: TwoPointers #5380

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 2 commits into from
Aug 25, 2024
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
28 changes: 11 additions & 17 deletions src/main/java/com/thealgorithms/others/TwoPointers.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
package com.thealgorithms.others;

import java.util.Arrays;

/**
* The two pointer technique is a useful tool to utilize when searching for
* 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/
* Link: https://www.geeksforgeeks.org/two-pointers-technique/
*/
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 key.
* 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.
*
* @param arr the array contains elements
* @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.
* @return {@code true} if there exists a pair of elements, {@code false} otherwise.
*/
public static boolean isPairedSum(int[] arr, int key) {
/* array sorting is necessary for this algorithm to function correctly */
Arrays.sort(arr);
int i = 0;
/* index of first element */
int j = arr.length - 1;
/* index of last element */
int i = 0; // index of the first element
int j = arr.length - 1; // index of the last element

while (i < j) {
if (arr[i] + arr[j] == key) {
int sum = arr[i] + arr[j];
if (sum == key) {
return true;
} else if (arr[i] + arr[j] < key) {
} else if (sum < key) {
i++;
} else {
j--;
Expand Down
50 changes: 39 additions & 11 deletions src/test/java/com/thealgorithms/others/TwoPointersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,65 @@
public class TwoPointersTest {

@Test
void twoPointersFirstTestCase() {
void testPositivePairExists() {
int[] arr = {2, 6, 9, 22, 121};
int key = 28;
assertTrue(TwoPointers.isPairedSum(arr, key));
}

@Test
void twoPointersSecondTestCase() {
int[] arr = {-1, -12, 12, 0, 8};
void testNegativePairExists() {
int[] arr = {-12, -1, 0, 8, 12};
int key = 0;
assertTrue(TwoPointers.isPairedSum(arr, key));
}

@Test
void twoPointersThirdTestCase() {
int[] arr = {12, 35, 12, 152, 0};
void testPairDoesNotExist() {
int[] arr = {0, 12, 12, 35, 152};
int key = 13;
assertFalse(TwoPointers.isPairedSum(arr, key));
}

@Test
void twoPointersFourthTestCase() {
int[] arr = {-2, 5, -1, 52, 31};
int key = -3;
void testNegativeSumPair() {
int[] arr = {-10, -3, 1, 2, 5, 9};
int key = -8;
assertTrue(TwoPointers.isPairedSum(arr, key));
}

@Test
void twoPointersFiftiethTestCase() {
int[] arr = {25, 1, 0, 61, 21};
int key = 12;
void testPairDoesNotExistWithPositiveSum() {
int[] arr = {1, 2, 3, 4, 5};
int key = 10;
assertFalse(TwoPointers.isPairedSum(arr, key));
}

@Test
void testEmptyArray() {
int[] arr = {};
int key = 5;
assertFalse(TwoPointers.isPairedSum(arr, key));
}

@Test
void testSingleElementArray() {
int[] arr = {5};
int key = 5;
assertFalse(TwoPointers.isPairedSum(arr, key));
}

@Test
void testArrayWithDuplicateElements() {
int[] arr = {1, 1, 3, 5, 5};
int key = 6;
assertTrue(TwoPointers.isPairedSum(arr, key));
}

@Test
void testPairExistsAtEdges() {
int[] arr = {1, 3, 4, 7, 8};
int key = 9;
assertTrue(TwoPointers.isPairedSum(arr, key));
}
}