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/C++/Diameter of a Binary Tree.cpp b/C++/Diameter of a Binary Tree.cpp new file mode 100644 index 0000000000..1fd2b037d2 --- /dev/null +++ b/C++/Diameter of a Binary Tree.cpp @@ -0,0 +1,17 @@ +class Solution { + public: + int ans=0; + int height(TreeNode* root){ + if(root==NULL){ + return 0; + } + int leftHt= height(root->left); + int rightHt= height(root->right); + ans=max(leftHt+rightHt,ans); + return max(leftHt,rightHt)+1; + } + int diameterOfBinaryTree(TreeNode* root) { + height(root); + return ans; + } + }; \ No newline at end of file diff --git a/C++/Lowest Common Ancestor of a Binary Tree.cpp b/C++/Lowest Common Ancestor of a Binary Tree.cpp new file mode 100644 index 0000000000..f63c8b6a3b --- /dev/null +++ b/C++/Lowest Common Ancestor of a Binary Tree.cpp @@ -0,0 +1,32 @@ +/* + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * }; +*/ +class Solution { + public: + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { + if(root==NULL){ + return NULL; + } + if(root->val==p->val || root->val==q->val){ + return root; + } + TreeNode* leftLCA=lowestCommonAncestor(root->left,p,q); + TreeNode* rightLCA=lowestCommonAncestor(root->right,p,q); + if(leftLCA && rightLCA) + { + return root; + } + else if(leftLCA!=NULL){ + return leftLCA; + } + else{ + return rightLCA; + } + } + }; \ No newline at end of file diff --git a/Java/.project b/Java/.project index 4fdb69a93e..d92baf7aec 100644 --- a/Java/.project +++ b/Java/.project @@ -14,4 +14,19 @@ org.eclipse.jdt.core.javanature + + +<<<<<<< HEAD + 1745466012044 +======= + 1744612223460 +>>>>>>> ccb4566f91334ed4ca8447e7932db4fa2b1a10d1 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/Java/Build Array from Permutation.java b/Java/Build Array from Permutation.java new file mode 100644 index 0000000000..d1c56f7581 --- /dev/null +++ b/Java/Build Array from Permutation.java @@ -0,0 +1,11 @@ +class Solution { + + public int[] buildArray(int[] nums) { + int n = nums.length; + int[] ans = new int[n]; + for (int i = 0; i < n; ++i) { + ans[i] = nums[nums[i]]; + } + return ans; + } +} 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