diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/Sum_Of_Subset.java b/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java similarity index 54% rename from src/main/java/com/thealgorithms/dynamicprogramming/Sum_Of_Subset.java rename to src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java index 90c07889..622f8b14 100644 --- a/src/main/java/com/thealgorithms/dynamicprogramming/Sum_Of_Subset.java +++ b/src/main/java/com/thealgorithms/dynamicprogramming/SumOfSubset.java @@ -1,17 +1,6 @@ package com.thealgorithms.dynamicprogramming; -public class Sum_Of_Subset { - - public static void main(String[] args) { - int[] arr = {7, 3, 2, 5, 8}; - int Key = 14; - - if (subsetSum(arr, arr.length - 1, Key)) { - System.out.print("Yes, that sum exists"); - } else { - System.out.print("Nope, that number does not exist"); - } - } +public class SumOfSubset { public static boolean subsetSum(int[] arr, int num, int Key) { if (Key == 0) { diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java new file mode 100644 index 00000000..53c34937 --- /dev/null +++ b/src/test/java/com/thealgorithms/dynamicprogramming/SumOfSubsetTest.java @@ -0,0 +1,17 @@ +package com.thealgorithms.dynamicprogramming; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +class SumOfSubsetTest { + + @Test + void basicCheck() { + assertEquals(false, SumOfSubset.subsetSum(new int[] {1, 2, 7, 10, 9}, 4, 14)); + assertEquals(false, SumOfSubset.subsetSum(new int[] {2, 15, 1, 6, 7}, 4, 4)); + assertEquals(true, SumOfSubset.subsetSum(new int[] {7, 3, 2, 5, 8}, 4, 14)); + assertEquals(true, SumOfSubset.subsetSum(new int[] {4, 3, 2, 1}, 3, 5)); + assertEquals(true, SumOfSubset.subsetSum(new int[] {1, 7, 2, 9, 10}, 4, 13)); + } +}