Skip to content

Commit 6cad7ce

Browse files
authored
Merge branch 'master' into test/PrimeFactorizationTest
2 parents 1fedef1 + a8d3b6a commit 6cad7ce

File tree

3 files changed

+61
-45
lines changed

3 files changed

+61
-45
lines changed

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

Lines changed: 11 additions & 17 deletions
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/maths/ReverseNumberTest.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,21 @@
33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertThrows;
55

6-
import java.util.HashMap;
7-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
8+
import org.junit.jupiter.params.provider.ValueSource;
89

910
public class ReverseNumberTest {
1011

11-
@Test
12-
public void testReverseNumber() {
13-
HashMap<Integer, Integer> testCases = new HashMap<>();
14-
testCases.put(0, 0);
15-
testCases.put(1, 1);
16-
testCases.put(10, 1);
17-
testCases.put(123, 321);
18-
testCases.put(7890, 987);
19-
20-
for (final var tc : testCases.entrySet()) {
21-
assertEquals(ReverseNumber.reverseNumber(tc.getKey()), tc.getValue());
22-
}
12+
@ParameterizedTest
13+
@CsvSource({"0, 0", "1, 1", "10, 1", "123, 321", "7890, 987"})
14+
public void testReverseNumber(int input, int expected) {
15+
assertEquals(expected, ReverseNumber.reverseNumber(input));
2316
}
2417

25-
@Test
26-
public void testReverseNumberThrowsExceptionForNegativeInput() {
27-
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(-1));
18+
@ParameterizedTest
19+
@ValueSource(ints = {-1, -123, -7890})
20+
public void testReverseNumberThrowsExceptionForNegativeInput(int input) {
21+
assertThrows(IllegalArgumentException.class, () -> ReverseNumber.reverseNumber(input));
2822
}
2923
}

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

Lines changed: 39 additions & 11 deletions
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)