Skip to content

Commit 3868844

Browse files
authored
refactor: TwoPointers (#5380)
1 parent b231a72 commit 3868844

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

src/main/java/com/thealgorithms/others/TwoPointers.java

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
11
package com.thealgorithms.others;
22

3-
import java.util.Arrays;
4-
53
/**
6-
* The two pointer technique is a useful tool to utilize when searching for
4+
* The two-pointer technique is a useful tool to utilize when searching for
75
* pairs in a sorted array.
86
*
97
* <p>
10-
* link: https://www.geeksforgeeks.org/two-pointers-technique/
8+
* Link: https://www.geeksforgeeks.org/two-pointers-technique/
119
*/
1210
final class TwoPointers {
1311
private TwoPointers() {
1412
}
1513

1614
/**
17-
* Given a sorted array arr (sorted in ascending order). Find if there
18-
* exists any pair of elements such that their sum is equal to key.
15+
* Given a sorted array arr (sorted in ascending order), find if there exists
16+
* any pair of elements such that their sum is equal to the key.
1917
*
20-
* @param arr the array contains elements
18+
* @param arr the array containing elements (must be sorted in ascending order)
2119
* @param key the number to search
22-
* @return {@code true} if there exists a pair of elements, {@code false}
23-
* otherwise.
20+
* @return {@code true} if there exists a pair of elements, {@code false} otherwise.
2421
*/
2522
public static boolean isPairedSum(int[] arr, int key) {
26-
/* array sorting is necessary for this algorithm to function correctly */
27-
Arrays.sort(arr);
28-
int i = 0;
29-
/* index of first element */
30-
int j = arr.length - 1;
31-
/* index of last element */
23+
int i = 0; // index of the first element
24+
int j = arr.length - 1; // index of the last element
3225

3326
while (i < j) {
34-
if (arr[i] + arr[j] == key) {
27+
int sum = arr[i] + arr[j];
28+
if (sum == key) {
3529
return true;
36-
} else if (arr[i] + arr[j] < key) {
30+
} else if (sum < key) {
3731
i++;
3832
} else {
3933
j--;

src/test/java/com/thealgorithms/others/TwoPointersTest.java

+39-11
Original file line numberDiff line numberDiff line change
@@ -8,37 +8,65 @@
88
public class TwoPointersTest {
99

1010
@Test
11-
void twoPointersFirstTestCase() {
11+
void testPositivePairExists() {
1212
int[] arr = {2, 6, 9, 22, 121};
1313
int key = 28;
1414
assertTrue(TwoPointers.isPairedSum(arr, key));
1515
}
1616

1717
@Test
18-
void twoPointersSecondTestCase() {
19-
int[] arr = {-1, -12, 12, 0, 8};
18+
void testNegativePairExists() {
19+
int[] arr = {-12, -1, 0, 8, 12};
2020
int key = 0;
2121
assertTrue(TwoPointers.isPairedSum(arr, key));
2222
}
2323

2424
@Test
25-
void twoPointersThirdTestCase() {
26-
int[] arr = {12, 35, 12, 152, 0};
25+
void testPairDoesNotExist() {
26+
int[] arr = {0, 12, 12, 35, 152};
2727
int key = 13;
2828
assertFalse(TwoPointers.isPairedSum(arr, key));
2929
}
3030

3131
@Test
32-
void twoPointersFourthTestCase() {
33-
int[] arr = {-2, 5, -1, 52, 31};
34-
int key = -3;
32+
void testNegativeSumPair() {
33+
int[] arr = {-10, -3, 1, 2, 5, 9};
34+
int key = -8;
3535
assertTrue(TwoPointers.isPairedSum(arr, key));
3636
}
3737

3838
@Test
39-
void twoPointersFiftiethTestCase() {
40-
int[] arr = {25, 1, 0, 61, 21};
41-
int key = 12;
39+
void testPairDoesNotExistWithPositiveSum() {
40+
int[] arr = {1, 2, 3, 4, 5};
41+
int key = 10;
4242
assertFalse(TwoPointers.isPairedSum(arr, key));
4343
}
44+
45+
@Test
46+
void testEmptyArray() {
47+
int[] arr = {};
48+
int key = 5;
49+
assertFalse(TwoPointers.isPairedSum(arr, key));
50+
}
51+
52+
@Test
53+
void testSingleElementArray() {
54+
int[] arr = {5};
55+
int key = 5;
56+
assertFalse(TwoPointers.isPairedSum(arr, key));
57+
}
58+
59+
@Test
60+
void testArrayWithDuplicateElements() {
61+
int[] arr = {1, 1, 3, 5, 5};
62+
int key = 6;
63+
assertTrue(TwoPointers.isPairedSum(arr, key));
64+
}
65+
66+
@Test
67+
void testPairExistsAtEdges() {
68+
int[] arr = {1, 3, 4, 7, 8};
69+
int key = 9;
70+
assertTrue(TwoPointers.isPairedSum(arr, key));
71+
}
4472
}

0 commit comments

Comments
 (0)