From bb830e9559648659697f17f449c51c6dc0661275 Mon Sep 17 00:00:00 2001 From: Rutikpatil0123 <71816232+Rutikpatil0123@users.noreply.github.com> Date: Tue, 2 May 2023 22:33:21 +0530 Subject: [PATCH] Add tests for TwoSumProblem and reduce duplication (fixes #4177) (#4176) --- .../com/thealgorithms/misc/TwoSumProblem.java | 109 ------------------ .../com/thealgorithms/others/TwoPointers.java | 17 +-- .../thealgorithms/others/TwoPointersTest.java | 44 +++++++ 3 files changed, 45 insertions(+), 125 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/misc/TwoSumProblem.java create mode 100644 src/test/java/com/thealgorithms/others/TwoPointersTest.java diff --git a/src/main/java/com/thealgorithms/misc/TwoSumProblem.java b/src/main/java/com/thealgorithms/misc/TwoSumProblem.java deleted file mode 100644 index e355cec02..000000000 --- a/src/main/java/com/thealgorithms/misc/TwoSumProblem.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.thealgorithms.misc; - -import java.util.*; -import java.util.stream.Collectors; - -public class TwoSumProblem { - - public static void main(String[] args) { - Scanner scan = new Scanner(System.in); - System.out.print("Enter the target sum "); - int ts = scan.nextInt(); - System.out.print("Enter the number of elements in the array "); - int n = scan.nextInt(); - System.out.println("Enter all your array elements:"); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) { - arr[i] = scan.nextInt(); - } - TwoSumProblem t = new TwoSumProblem(); - System.out.println( - "Brute Force Approach\n" + - Arrays.toString(t.BruteForce(arr, ts)) + - "\n" - ); - System.out.println( - "Two Pointer Approach\n" + - Arrays.toString(t.TwoPointer(arr, ts)) + - "\n" - ); - System.out.println( - "Hashmap Approach\n" + Arrays.toString(t.HashMap(arr, ts)) - ); - } - - public int[] BruteForce(int[] nums, int target) { - //Brute Force Approach - int[] ans = new int[2]; - for (int i = 0; i < nums.length; i++) { - for (int j = i + 1; j < nums.length; j++) { - if (nums[i] + nums[j] == target) { - ans[0] = i; - ans[1] = j; - - break; - } - } - } - - return ans; - } - - public int[] TwoPointer(int[] nums, int target) { - // HashMap Approach - int[] ans = new int[2]; - HashMap hm = new HashMap(); - for (int i = 0; i < nums.length; i++) { - hm.put(i, nums[i]); - } - HashMap temp = hm - .entrySet() - .stream() - .sorted((i1, i2) -> i1.getValue().compareTo(i2.getValue())) - .collect( - Collectors.toMap( - Map.Entry::getKey, - Map.Entry::getValue, - (e1, e2) -> e1, - LinkedHashMap::new - ) - ); - - int start = 0; - int end = nums.length - 1; - while (start < end) { - int currSum = (Integer) temp.values().toArray()[start] + - (Integer) temp.values().toArray()[end]; - - if (currSum == target) { - ans[0] = (Integer) temp.keySet().toArray()[start]; - ans[1] = (Integer) temp.keySet().toArray()[end]; - break; - } else if (currSum > target) { - end -= 1; - } else if (currSum < target) { - start += 1; - } - } - return ans; - } - - public int[] HashMap(int[] nums, int target) { - //Using Hashmaps - int[] ans = new int[2]; - HashMap hm = new HashMap(); - for (int i = 0; i < nums.length; i++) { - hm.put(nums[i], i); - } - for (int i = 0; i < nums.length; i++) { - int t = target - nums[i]; - if (hm.containsKey(t) && hm.get(t) != i) { - ans[0] = i; - ans[1] = hm.get(t); - break; - } - } - - return ans; - } -} diff --git a/src/main/java/com/thealgorithms/others/TwoPointers.java b/src/main/java/com/thealgorithms/others/TwoPointers.java index c5b57f344..de44354a6 100644 --- a/src/main/java/com/thealgorithms/others/TwoPointers.java +++ b/src/main/java/com/thealgorithms/others/TwoPointers.java @@ -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; diff --git a/src/test/java/com/thealgorithms/others/TwoPointersTest.java b/src/test/java/com/thealgorithms/others/TwoPointersTest.java new file mode 100644 index 000000000..7241140c7 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/TwoPointersTest.java @@ -0,0 +1,44 @@ +package com.thealgorithms.others; + +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; + + +public class TwoPointersTest { + + + @Test + void twoPointersFirstTestCase(){ + int[] arr = {2,6,9,22,121}; + int key = 28; + assertEquals(true, TwoPointers.isPairedSum(arr,key)); + } + + @Test + void twoPointersSecondTestCase(){ + int[] arr = {-1,-12,12,0,8}; + int key = 0; + assertEquals(true, TwoPointers.isPairedSum(arr,key)); + } + + @Test + void twoPointersThirdTestCase(){ + int[] arr = {12,35,12,152,0}; + int key = 13; + assertEquals(false, TwoPointers.isPairedSum(arr,key)); + } + + @Test + void twoPointersFourthTestCase(){ + int[] arr = {-2,5,-1,52,31}; + int key = -3; + assertEquals(true, TwoPointers.isPairedSum(arr,key)); + } + + @Test + void twoPointersFiftiethTestCase(){ + int[] arr = {25,1,0,61,21}; + int key = 12; + assertEquals(false, TwoPointers.isPairedSum(arr,key)); + } +}