diff --git a/Recursion/CombinationOne.java b/Recursion/CombinationOne.java new file mode 100644 index 0000000..7b75cf2 --- /dev/null +++ b/Recursion/CombinationOne.java @@ -0,0 +1,73 @@ +// https://leetcode.com/problems/combination-sum/ + +import java.util.*; + +class CombinationOne { + + public static void findCombination(int currentIndex, List ds, int candidates[], int n, int target, List> finalAnswer){ + + if (currentIndex == n){ + if(target == 0){ + finalAnswer.add(new ArrayList<>(ds)); + } + return; + } + + if (candidates[currentIndex] <= target){ + ds.add(candidates[currentIndex]); + + // Reuse same element + findCombination(currentIndex, ds, candidates, n, target - candidates[currentIndex], finalAnswer); + + ds.remove(ds.size() - 1); + } + + // Move to next index + findCombination(currentIndex + 1, ds, candidates, n, target, finalAnswer); + } + + public List> combinationSum(int[] candidates, int target) { + + int n = candidates.length; + + List> finalAnswer = new ArrayList<>(); + List ds = new ArrayList<>(); + + findCombination(0, ds, candidates, n, target, finalAnswer); + + return finalAnswer; + } +} + + +public class Main { + + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + System.out.print("Enter number of elements: "); + int n = sc.nextInt(); + + int[] candidates = new int[n]; + + System.out.println("Enter elements:"); + for(int i = 0; i < n; i++){ + candidates[i] = sc.nextInt(); + } + + System.out.print("Enter target: "); + int target = sc.nextInt(); + + Solution obj = new Solution(); + + List> result = obj.combinationSum(candidates, target); + + System.out.println("Possible combinations are:"); + for(List list : result){ + System.out.println(list); + } + + sc.close(); + } +} diff --git a/Recursion/CombinationTwo.java b/Recursion/CombinationTwo.java new file mode 100644 index 0000000..00c094a --- /dev/null +++ b/Recursion/CombinationTwo.java @@ -0,0 +1,70 @@ +// https://leetcode.com/problems/combination-sum-ii/ +import java.util.*; + +class CombinationTwo { + + public static void findCombination(int currentIndex, List ds, int candidates[], int target, List> finalAnswer){ + + if (target == 0){ + finalAnswer.add(new ArrayList<>(ds)); + return; + } + + for (int i = currentIndex; i < candidates.length; i++){ + + if (i > currentIndex && candidates[i] == candidates[i-1]) continue; + + if (candidates[i] > target) break; + + ds.add(candidates[i]); + + findCombination(i + 1, ds, candidates, target - candidates[i], finalAnswer); + + ds.remove(ds.size() - 1); + } + } + + public List> combinationSum2(int[] candidates, int target) { + + Arrays.sort(candidates); + + List> finalAnswer = new ArrayList<>(); + List ds = new ArrayList<>(); + + findCombination(0, ds, candidates, target, finalAnswer); + + return finalAnswer; + } +} + + +public class Main { + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + + System.out.print("Enter number of elements: "); + int n = sc.nextInt(); + + int[] candidates = new int[n]; + + System.out.println("Enter elements:"); + for(int i = 0; i < n; i++){ + candidates[i] = sc.nextInt(); + } + + System.out.print("Enter target: "); + int target = sc.nextInt(); + + Solution obj = new Solution(); + + List> result = obj.combinationSum2(candidates, target); + + System.out.println("Unique combinations are:"); + for(List list : result){ + System.out.println(list); + } + + sc.close(); + } +}