diff --git a/leetCode Solutions/Q39_CombinationSum/Q39_CombinationSum.java b/leetCode Solutions/Q39_CombinationSum/Q39_CombinationSum.java new file mode 100644 index 0000000..736f060 --- /dev/null +++ b/leetCode Solutions/Q39_CombinationSum/Q39_CombinationSum.java @@ -0,0 +1,24 @@ +/* This solution uses the concept of recursion by considering both the cases of idea of picking or not picking an element in the array to find the target sum */ + +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> result = new ArrayList<>(); + findCombination(0,candidates,result, new ArrayList<>(), target); + return result; + } + public void findCombination(int i, int arr[],List> result, ArrayList al,int target) { + if(i== arr.length){ + if(target == 0) result.add(new ArrayList<>(al)); + return; + } + if(arr[i]<=target){ + //pick single element multiple times + al.add(arr[i]); + //we are not changing index i, because we are picking same element multiple times + findCombination(i,arr,result,al, target-arr[i]); + al.remove(al.size()-1); + } + //moving to next index and start again with pick/not-pick principle + findCombination(i+1,arr,result, al, target); + } +}