diff --git a/C++/Combination Sum.cpp b/C++/Combination Sum.cpp new file mode 100644 index 0000000000..6c9ffc221d --- /dev/null +++ b/C++/Combination Sum.cpp @@ -0,0 +1,49 @@ +#include +#include + +class Solution { + + public: + + vector> combinationSum(vector& candidates, int target) { + + vector> res; + + vector comb; + + makeCombination(candidates, target, 0, comb, 0, res); + + return res; + + } + + private: + + void makeCombination(std::vector& candidates, int target, int idx, vector& comb, int total, vector>& res) { + + if (total == target) { + + res.push_back(comb); + + return; + + } + + if (total > target || idx >= candidates.size()) { + + return; + + } + + comb.push_back(candidates[idx]); + + makeCombination(candidates, target, idx, comb, total + candidates[idx], res); + + comb.pop_back(); + + makeCombination(candidates, target, idx + 1, comb, total, res); + + } + + }; + \ No newline at end of file diff --git a/Java/.project b/Java/.project index 4fdb69a93e..e4eed73892 100644 --- a/Java/.project +++ b/Java/.project @@ -14,4 +14,15 @@ org.eclipse.jdt.core.javanature + + + 1744612223460 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Java/Count Equal and Divisible in an Array.java b/Java/Count Equal and Divisible in an Array.java new file mode 100644 index 0000000000..22d9f82330 --- /dev/null +++ b/Java/Count Equal and Divisible in an Array.java @@ -0,0 +1,17 @@ +class Solution { + public int countPairs(int[] nums, int k) { + int n=nums.length; + int res=0; + for(int i=0;i