diff --git a/Add Two Numbers.java b/Add Two Numbers.java new file mode 100644 index 0000000000..2a5b96c1b1 --- /dev/null +++ b/Add Two Numbers.java @@ -0,0 +1,22 @@ +class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode dummy = new ListNode(); + ListNode cur=dummy; + int carry=0; + while(l1!=null || l2!=null || carry!=0){ + int sum=carry; + if(l1!=null){ + sum +=l1.val; + l1=l1.next; + } + if (l2 != null) { + sum += l2.val; + l2 = l2.next; + } + carry = sum / 10; + cur.next = new ListNode(sum % 10); + cur = cur.next; + } + return dummy.next; + } +} 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/C++/Palindrome Number.cpp b/C++/Palindrome Number.cpp new file mode 100644 index 0000000000..40a8b56aad --- /dev/null +++ b/C++/Palindrome Number.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int reverse(int x){ + int revNum=0; + while(x!=0) + { + int dig=x%10; + if(revNum>INT_MAX/10 || revNumv(numRows, ""); + + int j = 0, dir = -1; + + for(int i = 0; i < s.length(); i++) + { + + if(j == numRows - 1 || j == 0) dir *= (-1); + + v[j] += s[i]; + + if(dir == 1) j++; + + else j--; + } + + string res; + + for(auto &it : v) res += it; + + return res; + + } +}; diff --git a/Count Number of Balanced Permutation.java b/Count Number of Balanced Permutation.java new file mode 100644 index 0000000000..c05bf18993 --- /dev/null +++ b/Count Number of Balanced Permutation.java @@ -0,0 +1,69 @@ +class Solution { + + private static final long MOD = 1_000_000_007; + + public int countBalancedPermutations(String num) { + int tot = 0, n = num.length(); + int[] cnt = new int[10]; + for (char ch : num.toCharArray()) { + int d = ch - '0'; + cnt[d]++; + tot += d; + } + if (tot % 2 != 0) { + return 0; + } + + int target = tot / 2; + int maxOdd = (n + 1) / 2; + long[][] comb = new long[maxOdd + 1][maxOdd + 1]; + long[][] f = new long[target + 1][maxOdd + 1]; + + for (int i = 0; i <= maxOdd; i++) { + comb[i][i] = comb[i][0] = 1; + for (int j = 1; j < i; j++) { + comb[i][j] = (comb[i - 1][j] + comb[i - 1][j - 1]) % MOD; + } + } + + f[0][0] = 1; + int psum = 0, totSum = 0; + for (int i = 0; i <= 9; i++) { + /* Sum of the number of the first i digits */ + psum += cnt[i]; + /* Sum of the first i numbers */ + totSum += i * cnt[i]; + for ( + int oddCnt = Math.min(psum, maxOdd); + oddCnt >= Math.max(0, psum - (n - maxOdd)); + oddCnt-- + ) { + /* The number of bits that need to be filled in even numbered positions */ + int evenCnt = psum - oddCnt; + for ( + int curr = Math.min(totSum, target); + curr >= Math.max(0, totSum - target); + curr-- + ) { + long res = 0; + for ( + int j = Math.max(0, cnt[i] - evenCnt); + j <= Math.min(cnt[i], oddCnt) && i * j <= curr; + j++ + ) { + /* The current digit is filled with j positions at odd positions, and cnt[i] - j positions at even positions */ + long ways = + (comb[oddCnt][j] * comb[evenCnt][cnt[i] - j]) % MOD; + res = + (res + + ((ways * f[curr - i * j][oddCnt - j]) % MOD)) % + MOD; + } + f[curr][oddCnt] = res % MOD; + } + } + } + + return (int) f[target][maxOdd]; + } +} diff --git a/Divisible And Non-divisible Sum Differences.java b/Divisible And Non-divisible Sum Differences.java new file mode 100644 index 0000000000..6e63287990 --- /dev/null +++ b/Divisible And Non-divisible Sum Differences.java @@ -0,0 +1,13 @@ +class Solution { + public int differenceOfSums(int n, int m) { + int ans=0; + for(int i=1;i<=n;i++){ + if(i % m ==0){ + ans-=i; + }else{ + ans+=i; + } + } + return ans; + } +} diff --git a/Find Words Containing Character.java b/Find Words Containing Character.java new file mode 100644 index 0000000000..6d5cbed9ca --- /dev/null +++ b/Find Words Containing Character.java @@ -0,0 +1,13 @@ +class Solution { + public List findWordsContaining(String[] words, char x) { + List res=new ArrayList<>(); + int n=words.length; + for(int i=0;i org.eclipse.jdt.core.javanature + + + + + 1744612223460 + + + 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/Candy.java b/Java/Candy.java new file mode 100644 index 0000000000..2c29a5df20 --- /dev/null +++ b/Java/Candy.java @@ -0,0 +1,29 @@ +class Solution { + public int candy(int[] ratings) { + int n = ratings.length; + int[] cand = new int[n]; + Arrays.fill(cand, 1); // Step 1: Each child gets at least one candy + + // Step 2: Left to right pass + for (int i = 1; i < n; i++) { + if (ratings[i] > ratings[i - 1]) { + cand[i] = cand[i - 1] + 1; + } + } + + // Step 3: Right to left pass + for (int i = n - 2; i >= 0; i--) { + if (ratings[i] > ratings[i + 1] && cand[i] <= cand[i + 1]) { + cand[i] = cand[i + 1] + 1; + } + } + + // Step 4: Sum all candies + int ans = 0; + for (int c : cand) { + ans += c; + } + + 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