Add tests for TwoSumProblem and reduce duplication (fixes #4177) (#4176)

This commit is contained in:
Rutikpatil0123
2023-05-02 22:33:21 +05:30
committed by GitHub
parent fb18c27905
commit bb830e9559
3 changed files with 45 additions and 125 deletions

View File

@ -11,21 +11,6 @@ import java.util.Arrays;
*/
class TwoPointers {
public static void main(String[] args) {
int[] arr = { 10, 20, 35, 50, 75, 80 };
int key = 70;
assert isPairedSum(arr, key);
/* 20 + 60 == 70 */
arr = new int[] { 1, 2, 3, 4, 5, 6, 7 };
key = 13;
assert isPairedSum(arr, key);
/* 6 + 7 == 13 */
key = 14;
assert !isPairedSum(arr, 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 key.
@ -35,7 +20,7 @@ class TwoPointers {
* @return {@code true} if there exists a pair of elements, {@code false}
* otherwise.
*/
private static boolean isPairedSum(int[] arr, int key) {
public static boolean isPairedSum(int[] arr, int key) {
/* array sorting is necessary for this algorithm to function correctly */
Arrays.sort(arr);
int i = 0;