From d5ab1dda55fc2fadda95eec4d995c1c9d1182ce0 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 14 Apr 2025 12:03:51 +0530
Subject: [PATCH 01/58] Add maximum value of Ordered Triplets
---
Java/.project | 11 ++++++++
...Maximum Value of an Ordered Triplets.java | 28 +++++++++++++++++++
2 files changed, 39 insertions(+)
create mode 100644 Java/Maximum Value of an Ordered Triplets.java
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/Maximum Value of an Ordered Triplets.java b/Java/Maximum Value of an Ordered Triplets.java
new file mode 100644
index 0000000000..0d66520084
--- /dev/null
+++ b/Java/Maximum Value of an Ordered Triplets.java
@@ -0,0 +1,28 @@
+
+class Solution {
+ public long maximumTripletValue(int[] nums){
+ int n = nums.length;
+ if (n < 3) return 0;
+
+ int[] leftMax = new int[n];
+ leftMax[0] = nums[0];
+ for (int i = 1; i < n; i++) {
+ leftMax[i] = Math.max(leftMax[i - 1], nums[i]);
+ }
+
+ int[] rightMax = new int[n];
+ rightMax[n - 1] = nums[n - 1];
+ for (int i = n - 2; i >= 0; i--) {
+ rightMax[i] = Math.max(rightMax[i + 1], nums[i]);
+ }
+
+ long ans = 0;
+ for (int i = 1; i < n - 1; i++) {
+ int left = leftMax[i - 1];
+ int right = rightMax[i + 1];
+ ans = Math.max(ans, (long)(left - nums[i]) * right);
+ }
+
+ return ans;
+ }
+}
\ No newline at end of file
From 797743ff1291441a8bd7c07c72ba619336e33b46 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 14 Apr 2025 12:08:51 +0530
Subject: [PATCH 02/58] U
---
Java/Maximum Value of an Ordered Triplets.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/Java/Maximum Value of an Ordered Triplets.java b/Java/Maximum Value of an Ordered Triplets.java
index 0d66520084..1453d1b089 100644
--- a/Java/Maximum Value of an Ordered Triplets.java
+++ b/Java/Maximum Value of an Ordered Triplets.java
@@ -1,3 +1,5 @@
+ //TC:O(N)
+ //SC:O(N)
class Solution {
public long maximumTripletValue(int[] nums){
From 3cabcced8a53ab5de09d26c13d3b9f86afc26f53 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 15 Apr 2025 10:43:30 +0530
Subject: [PATCH 03/58] Change
---
C++/Combination Sum.cpp | 49 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 49 insertions(+)
create mode 100644 C++/Combination Sum.cpp
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
From 3c135ec3223ba13d545476793573536f56222e67 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 16 Apr 2025 14:49:36 +0530
Subject: [PATCH 04/58] Create Count of Good Subarrays.java
---
Java/Count of Good Subarray.java | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
create mode 100644 Java/Count of Good Subarray.java
diff --git a/Java/Count of Good Subarray.java b/Java/Count of Good Subarray.java
new file mode 100644
index 0000000000..da19d185c5
--- /dev/null
+++ b/Java/Count of Good Subarray.java
@@ -0,0 +1,22 @@
+public class Solution {
+
+ public long countGood(int[] nums, int k) {
+ int n = nums.length;
+ int same = 0, right = -1;
+ HashMap cnt = new HashMap<>();
+ long ans = 0;
+ for (int left = 0; left < n; ++left) {
+ while (same < k && right + 1 < n) {
+ ++right;
+ same += cnt.getOrDefault(nums[right], 0);
+ cnt.put(nums[right], cnt.getOrDefault(nums[right], 0) + 1);
+ }
+ if (same >= k) {
+ ans += n - right;
+ }
+ cnt.put(nums[left], cnt.get(nums[left]) - 1);
+ same -= cnt.get(nums[left]);
+ }
+ return ans;
+ }
+}
From 1aa98bec7600cb53e83dd12531056f97b96f1d17 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 17 Apr 2025 09:37:57 +0530
Subject: [PATCH 05/58] Add Create Equal and Divisible in an Array
---
Java/Count Equal and Divisible in an Array.java | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 Java/Count Equal and Divisible in an Array.java
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
Date: Mon, 21 Apr 2025 20:31:48 +0530
Subject: [PATCH 06/58] Update
---
Java/Count Hidden Sequence.java | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 Java/Count Hidden Sequence.java
diff --git a/Java/Count Hidden Sequence.java b/Java/Count Hidden Sequence.java
new file mode 100644
index 0000000000..5dc354b7f9
--- /dev/null
+++ b/Java/Count Hidden Sequence.java
@@ -0,0 +1,18 @@
+public class Solution {
+ public int numberOfArrays(int[] differences, int lower, int upper) {
+ int n = differences.length;
+ int prefix = 0;
+ int min = 0;
+ int max = 0;
+ int diff = upper-lower;
+ for(int i = 0;i diff) return 0;
+ }
+ return (diff) - (max -min) +1;
+ }
+}
+
+
From 5e4c852cd8174e09303f46618889ab233231b656 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 21 Apr 2025 20:32:58 +0530
Subject: [PATCH 07/58] u
---
Java/Count Hidden Sequence.java | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Java/Count Hidden Sequence.java b/Java/Count Hidden Sequence.java
index 5dc354b7f9..c398d16df7 100644
--- a/Java/Count Hidden Sequence.java
+++ b/Java/Count Hidden Sequence.java
@@ -1,10 +1,11 @@
-public class Solution {
+public class Solution {
public int numberOfArrays(int[] differences, int lower, int upper) {
int n = differences.length;
int prefix = 0;
int min = 0;
int max = 0;
int diff = upper-lower;
+
for(int i = 0;i
Date: Tue, 22 Apr 2025 07:49:25 +0530
Subject: [PATCH 08/58] Update
---
Java/Count and Say.java | 75 +++++++++++++++++++++++++++++++++++++++++
1 file changed, 75 insertions(+)
create mode 100644 Java/Count and Say.java
diff --git a/Java/Count and Say.java b/Java/Count and Say.java
new file mode 100644
index 0000000000..bdebba3d63
--- /dev/null
+++ b/Java/Count and Say.java
@@ -0,0 +1,75 @@
+class Combinations {
+ long[] fact;
+ long[] invFact;
+ long M;
+
+ public Combinations(int n, long mod) {
+ M = mod;
+ fact = new long[n + 1];
+ invFact = new long[n + 1];
+ fact[0] = 1;
+ invFact[0] = 1;
+ for (int i = 1; i <= n; i++) {
+ fact[i] = (fact[i - 1] * i) % M;
+ invFact[i] = power(fact[i], M - 2);
+ }
+ }
+
+ long power(long base, long exp) {
+ long res = 1;
+ base %= M;
+ while (exp > 0) {
+ if ((exp & 1) == 1) res = (res * base) % M;
+ base = (base * base) % M;
+ exp >>= 1;
+ }
+ return res;
+ }
+
+ long nCr(int n, int r) {
+ if (r < 0 || r > n) return 0;
+ long num = fact[n];
+ long den = (invFact[r] * invFact[n - r]) % M;
+ return (num * den) % M;
+ }
+}
+
+class Solution {
+ private static final int MOD = 1_000_000_007;
+
+ public int idealArrays(int n, int maxValue) {
+ Combinations comb = new Combinations(n, MOD);
+ long[] dp = new long[maxValue + 1];
+ long totalAns = maxValue;
+
+ for (int i = 1; i <= maxValue; i++) {
+ dp[i] = 1;
+ }
+
+ int kLimit = Math.min(n, 16);
+
+ for (int k = 2; k <= kLimit; k++) {
+ long[] next_dp = new long[maxValue + 1];
+ for (int j = 1; j <= maxValue; j++) {
+ if (dp[j] == 0) continue;
+ for (long i = 2L * j; i <= maxValue; i += j) {
+ next_dp[(int) i] = (next_dp[(int) i] + dp[j]) % MOD;
+ }
+ }
+
+ long count = 0;
+ for (int i = 1; i <= maxValue; i++) {
+ count = (count + next_dp[i]) % MOD;
+ }
+
+ if (count == 0) break;
+
+ long factor = comb.nCr(n - 1, k - 1);
+ totalAns = (totalAns + count * factor % MOD) % MOD;
+
+ dp = next_dp;
+ }
+
+ return (int) totalAns;
+ }
+}
\ No newline at end of file
From a3f2228ec97790562196434b1e97315b59da14d6 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 23 Apr 2025 08:33:16 +0530
Subject: [PATCH 09/58] Change
---
Java/Count Largest Group.java | 26 ++++++++++++++++++++++++++
1 file changed, 26 insertions(+)
create mode 100644 Java/Count Largest Group.java
diff --git a/Java/Count Largest Group.java b/Java/Count Largest Group.java
new file mode 100644
index 0000000000..30507221a2
--- /dev/null
+++ b/Java/Count Largest Group.java
@@ -0,0 +1,26 @@
+class Solution {
+ public int countLargestGroup(int n) {
+ MaphashMap=new HashMap();
+ int maxValue=0;
+ for(int i=1;i<=n;i++)
+ {
+ int key=0,num=i;
+ while(num!=0)
+ {
+ key+=num%10;
+ num=num/10;
+ }
+ hashMap.put(key,hashMap.getOrDefault(key,0)+1);
+ maxValue=Math.max(maxValue,hashMap.get(key));
+ }
+ int count=0;
+ for(Map.Entrykvpair: hashMap.entrySet()){
+ if(kvpair.getValue()== maxValue)
+ {
+ ++count;
+ }
+ }
+ return count;
+
+ }
+}
\ No newline at end of file
From 3fbaf2922a07cefba84d09d42036181227cab6d3 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 24 Apr 2025 09:11:04 +0530
Subject: [PATCH 10/58] Update
---
Java/.project | 11 ++++++++++
Java/Count of Subarrays in An Array.java | 28 ++++++++++++++++++++++++
2 files changed, 39 insertions(+)
create mode 100644 Java/Count of Subarrays in An Array.java
diff --git a/Java/.project b/Java/.project
index 4fdb69a93e..bf31459ef4 100644
--- a/Java/.project
+++ b/Java/.project
@@ -14,4 +14,15 @@
org.eclipse.jdt.core.javanature
+
+
+ 1745466012044
+
+ 30
+
+ org.eclipse.core.resources.regexFilterMatcher
+ node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__
+
+
+
diff --git a/Java/Count of Subarrays in An Array.java b/Java/Count of Subarrays in An Array.java
new file mode 100644
index 0000000000..5032b24ab9
--- /dev/null
+++ b/Java/Count of Subarrays in An Array.java
@@ -0,0 +1,28 @@
+class Solution {
+ public int countCompleteSubarrays(int[] nums) {
+ int cnt = 0;
+ HashSet set = new HashSet<>();
+ for (int i = 0; i < nums.length; i++) {
+ set.add(nums[i]);
+ }
+ HashMap map = new HashMap<>();
+ int i = 0, j = 0;
+ while (j < nums.length) {
+ map.put(nums[j], map.getOrDefault(nums[j], 0) + 1);
+ if (map.size() == set.size()) {
+ cnt += nums.length - j;
+ while (true) {
+ map.put(nums[i], map.get(nums[i]) - 1);
+ if (map.get(nums[i]) == 0) {
+ map.remove(nums[i++]);
+ break;
+ }
+ cnt += nums.length - j;
+ i++;
+ }
+ }
+ j++;
+ }
+ return cnt;
+ }
+}
\ No newline at end of file
From 957c010a714e157136690ec469a85b248f9c703c Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 5 May 2025 19:26:51 +0530
Subject: [PATCH 11/58] Domino
---
C++/Combination Sum.cpp | 49 ++++++++++++
C++/Diameter of a Binary Tree.cpp | 17 +++++
...owest Common Ancestor of a Binary Tree.cpp | 32 ++++++++
Java/.project | 4 +
...Count Equal and Divisible in an Array.java | 17 +++++
Java/Count Hidden Sequence.java | 19 +++++
Java/Count Largest Group.java | 26 +++++++
...rays of Length 3 within the condition.java | 27 +++++++
...Where max element appear more than k .java | 19 +++++
Java/Count and Say.java | 75 +++++++++++++++++++
Java/Count of Good Subarray.java | 22 ++++++
Java/Count of Interested Subarrays.java | 32 ++++++++
...nt of Subarray with Score less than k.java | 15 ++++
...d Numbers with Even Numbers of Digits.java | 23 ++++++
...Maximum Value of an Ordered Triplets.java | 30 ++++++++
...nimum Dominaeo Rotation for Equal row.java | 47 ++++++++++++
16 files changed, 454 insertions(+)
create mode 100644 C++/Combination Sum.cpp
create mode 100644 C++/Diameter of a Binary Tree.cpp
create mode 100644 C++/Lowest Common Ancestor of a Binary Tree.cpp
create mode 100644 Java/Count Equal and Divisible in an Array.java
create mode 100644 Java/Count Hidden Sequence.java
create mode 100644 Java/Count Largest Group.java
create mode 100644 Java/Count Number of Subarrays of Length 3 within the condition.java
create mode 100644 Java/Count Subarray Where max element appear more than k .java
create mode 100644 Java/Count and Say.java
create mode 100644 Java/Count of Good Subarray.java
create mode 100644 Java/Count of Interested Subarrays.java
create mode 100644 Java/Count of Subarray with Score less than k.java
create mode 100644 Java/Find Numbers with Even Numbers of Digits.java
create mode 100644 Java/Maximum Value of an Ordered Triplets.java
create mode 100644 Java/Minimum Dominaeo Rotation for Equal row.java
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 bf31459ef4..d92baf7aec 100644
--- a/Java/.project
+++ b/Java/.project
@@ -16,7 +16,11 @@
+<<<<<<< HEAD
1745466012044
+=======
+ 1744612223460
+>>>>>>> ccb4566f91334ed4ca8447e7932db4fa2b1a10d1
30
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 diff) return 0;
+ }
+ return (diff) - (max -min) +1;
+ }
+}
+
+
diff --git a/Java/Count Largest Group.java b/Java/Count Largest Group.java
new file mode 100644
index 0000000000..30507221a2
--- /dev/null
+++ b/Java/Count Largest Group.java
@@ -0,0 +1,26 @@
+class Solution {
+ public int countLargestGroup(int n) {
+ MaphashMap=new HashMap();
+ int maxValue=0;
+ for(int i=1;i<=n;i++)
+ {
+ int key=0,num=i;
+ while(num!=0)
+ {
+ key+=num%10;
+ num=num/10;
+ }
+ hashMap.put(key,hashMap.getOrDefault(key,0)+1);
+ maxValue=Math.max(maxValue,hashMap.get(key));
+ }
+ int count=0;
+ for(Map.Entrykvpair: hashMap.entrySet()){
+ if(kvpair.getValue()== maxValue)
+ {
+ ++count;
+ }
+ }
+ return count;
+
+ }
+}
\ No newline at end of file
diff --git a/Java/Count Number of Subarrays of Length 3 within the condition.java b/Java/Count Number of Subarrays of Length 3 within the condition.java
new file mode 100644
index 0000000000..1fcbe2a495
--- /dev/null
+++ b/Java/Count Number of Subarrays of Length 3 within the condition.java
@@ -0,0 +1,27 @@
+public class Solution
+{
+ public int countSubarrays(int[] nums)
+ {
+ int count = 0;
+
+ // Step 1: Loop through the array to check subarrays of size 3
+ for (int i = 0; i <= nums.length - 3; i++)
+ {
+ // Step 2: Extract the three consecutive elements
+ int first = nums[i];
+ int second = nums[i + 1];
+ int third = nums[i + 2];
+
+ // Step 3: Check if 2 * (first + third) == second
+ if (2 * (first + third) == second)
+ {
+ count++; // Increment count if condition is satisfied
+ }
+ }
+
+ // Step 4: Return the final count of subarrays that satisfy the condition
+ return count;
+ //he.ll
+ }
+
+}
diff --git a/Java/Count Subarray Where max element appear more than k .java b/Java/Count Subarray Where max element appear more than k .java
new file mode 100644
index 0000000000..1557a4e9d1
--- /dev/null
+++ b/Java/Count Subarray Where max element appear more than k .java
@@ -0,0 +1,19 @@
+class Solution {
+ public long countSubarrays(int[] nums, int k) {
+ int n = nums.length;
+ int maxVal = 0;
+ for (int v : nums) maxVal = Math.max(maxVal, v);
+
+ long res = 0;
+ int count = 0, left = 0;
+ for (int right = 0; right < n; right++) {
+ if (nums[right] == maxVal) count++;
+ while (count >= k) {
+ if (nums[left] == maxVal) count--;
+ left++;
+ }
+ res += left;
+ }
+ return res;
+ }
+}
diff --git a/Java/Count and Say.java b/Java/Count and Say.java
new file mode 100644
index 0000000000..bdebba3d63
--- /dev/null
+++ b/Java/Count and Say.java
@@ -0,0 +1,75 @@
+class Combinations {
+ long[] fact;
+ long[] invFact;
+ long M;
+
+ public Combinations(int n, long mod) {
+ M = mod;
+ fact = new long[n + 1];
+ invFact = new long[n + 1];
+ fact[0] = 1;
+ invFact[0] = 1;
+ for (int i = 1; i <= n; i++) {
+ fact[i] = (fact[i - 1] * i) % M;
+ invFact[i] = power(fact[i], M - 2);
+ }
+ }
+
+ long power(long base, long exp) {
+ long res = 1;
+ base %= M;
+ while (exp > 0) {
+ if ((exp & 1) == 1) res = (res * base) % M;
+ base = (base * base) % M;
+ exp >>= 1;
+ }
+ return res;
+ }
+
+ long nCr(int n, int r) {
+ if (r < 0 || r > n) return 0;
+ long num = fact[n];
+ long den = (invFact[r] * invFact[n - r]) % M;
+ return (num * den) % M;
+ }
+}
+
+class Solution {
+ private static final int MOD = 1_000_000_007;
+
+ public int idealArrays(int n, int maxValue) {
+ Combinations comb = new Combinations(n, MOD);
+ long[] dp = new long[maxValue + 1];
+ long totalAns = maxValue;
+
+ for (int i = 1; i <= maxValue; i++) {
+ dp[i] = 1;
+ }
+
+ int kLimit = Math.min(n, 16);
+
+ for (int k = 2; k <= kLimit; k++) {
+ long[] next_dp = new long[maxValue + 1];
+ for (int j = 1; j <= maxValue; j++) {
+ if (dp[j] == 0) continue;
+ for (long i = 2L * j; i <= maxValue; i += j) {
+ next_dp[(int) i] = (next_dp[(int) i] + dp[j]) % MOD;
+ }
+ }
+
+ long count = 0;
+ for (int i = 1; i <= maxValue; i++) {
+ count = (count + next_dp[i]) % MOD;
+ }
+
+ if (count == 0) break;
+
+ long factor = comb.nCr(n - 1, k - 1);
+ totalAns = (totalAns + count * factor % MOD) % MOD;
+
+ dp = next_dp;
+ }
+
+ return (int) totalAns;
+ }
+}
\ No newline at end of file
diff --git a/Java/Count of Good Subarray.java b/Java/Count of Good Subarray.java
new file mode 100644
index 0000000000..da19d185c5
--- /dev/null
+++ b/Java/Count of Good Subarray.java
@@ -0,0 +1,22 @@
+public class Solution {
+
+ public long countGood(int[] nums, int k) {
+ int n = nums.length;
+ int same = 0, right = -1;
+ HashMap cnt = new HashMap<>();
+ long ans = 0;
+ for (int left = 0; left < n; ++left) {
+ while (same < k && right + 1 < n) {
+ ++right;
+ same += cnt.getOrDefault(nums[right], 0);
+ cnt.put(nums[right], cnt.getOrDefault(nums[right], 0) + 1);
+ }
+ if (same >= k) {
+ ans += n - right;
+ }
+ cnt.put(nums[left], cnt.get(nums[left]) - 1);
+ same -= cnt.get(nums[left]);
+ }
+ return ans;
+ }
+}
diff --git a/Java/Count of Interested Subarrays.java b/Java/Count of Interested Subarrays.java
new file mode 100644
index 0000000000..5f904d73fd
--- /dev/null
+++ b/Java/Count of Interested Subarrays.java
@@ -0,0 +1,32 @@
+class Solution
+{
+ public long countInterestingSubarrays(List nums, int modulo, int k)
+ {
+ // Step 1 : Initialize result, prefix sum, and a map with base case
+ long result = 0;
+ int prefix = 0;
+ Map map = new HashMap<>();
+ map.put(0, 1L);
+
+ // Step 2 : Traverse through nums
+ for (int num : nums)
+ {
+ // Step 3 : Check condition and update prefix
+ if (num % modulo == k)
+ {
+ prefix++;
+ }
+
+ // Step 4 : Calculate current prefix mod and target
+ int mod = prefix % modulo;
+ int target = (mod - k + modulo) % modulo;
+
+ // Step 5 : Add to result and update map
+ result += map.getOrDefault(target, 0L);
+ map.put(mod, map.getOrDefault(mod, 0L) + 1);
+ }
+
+ // Step 6 : Return final count
+ return result;
+ }
+}
diff --git a/Java/Count of Subarray with Score less than k.java b/Java/Count of Subarray with Score less than k.java
new file mode 100644
index 0000000000..9536523b93
--- /dev/null
+++ b/Java/Count of Subarray with Score less than k.java
@@ -0,0 +1,15 @@
+class Solution {
+ public long countSubarrays(int[] nums, long k) {
+ int n=nums.length;
+ long res=0,total=0;
+ for(int i=0,j=0;j=k){
+ total-=nums[i];
+ i++;
+ }
+ res+=j-i+1;
+ }
+ return res;
+ }
+}
\ No newline at end of file
diff --git a/Java/Find Numbers with Even Numbers of Digits.java b/Java/Find Numbers with Even Numbers of Digits.java
new file mode 100644
index 0000000000..abe01753f6
--- /dev/null
+++ b/Java/Find Numbers with Even Numbers of Digits.java
@@ -0,0 +1,23 @@
+class Solution {
+ // Helper function to check if the number of digits is even
+ private boolean hasEvenDigits(int num) {
+ int digitCount = 0;
+ while (num != 0) {
+ digitCount++;
+ num /= 10;
+ }
+ return (digitCount & 1) == 0;
+ }
+
+ public int findNumbers(int[] nums) {
+ // Counter to count the number of even digit integers
+ int evenDigitCount = 0;
+
+ for (int num : nums) {
+ if (hasEvenDigits(num))
+ evenDigitCount++;
+ }
+
+ return evenDigitCount;
+ }
+}
diff --git a/Java/Maximum Value of an Ordered Triplets.java b/Java/Maximum Value of an Ordered Triplets.java
new file mode 100644
index 0000000000..1453d1b089
--- /dev/null
+++ b/Java/Maximum Value of an Ordered Triplets.java
@@ -0,0 +1,30 @@
+ //TC:O(N)
+ //SC:O(N)
+
+class Solution {
+ public long maximumTripletValue(int[] nums){
+ int n = nums.length;
+ if (n < 3) return 0;
+
+ int[] leftMax = new int[n];
+ leftMax[0] = nums[0];
+ for (int i = 1; i < n; i++) {
+ leftMax[i] = Math.max(leftMax[i - 1], nums[i]);
+ }
+
+ int[] rightMax = new int[n];
+ rightMax[n - 1] = nums[n - 1];
+ for (int i = n - 2; i >= 0; i--) {
+ rightMax[i] = Math.max(rightMax[i + 1], nums[i]);
+ }
+
+ long ans = 0;
+ for (int i = 1; i < n - 1; i++) {
+ int left = leftMax[i - 1];
+ int right = rightMax[i + 1];
+ ans = Math.max(ans, (long)(left - nums[i]) * right);
+ }
+
+ return ans;
+ }
+}
\ No newline at end of file
diff --git a/Java/Minimum Dominaeo Rotation for Equal row.java b/Java/Minimum Dominaeo Rotation for Equal row.java
new file mode 100644
index 0000000000..eba0c9432a
--- /dev/null
+++ b/Java/Minimum Dominaeo Rotation for Equal row.java
@@ -0,0 +1,47 @@
+class Solution
+{
+ public int minDominoRotations(int[] tops, int[] bottoms)
+ {
+ // Step 1: Try to make all values equal to tops[0]
+ int result = check(tops[0], tops, bottoms);
+
+ // Step 2: If tops[0] failed, try bottoms[0]
+ if (result != -1) return result;
+
+ // Step 3: Return result from checking bottoms[0]
+ return check(bottoms[0], tops, bottoms);
+ }
+
+ // Step 4: Helper function to count rotations to make all values = target
+ private int check(int target, int[] tops, int[] bottoms)
+ {
+ int rotateTop = 0; // Rotations needed to bring target to top
+ int rotateBottom = 0; // Rotations needed to bring target to bottom
+
+ // Step 5: Loop through all dominoes
+ for (int i = 0; i < tops.length; i++)
+ {
+ // Step 6: If target is not on either side, it's impossible
+ if (tops[i] != target && bottoms[i] != target)
+ {
+ return -1;
+ }
+
+ // Step 7: If top doesn't have the target, it must be rotated
+ if (tops[i] != target)
+ {
+ rotateTop++;
+ }
+
+ // Step 8: If bottom doesn't have the target, it must be rotated
+ if (bottoms[i] != target)
+ {
+ rotateBottom++;
+ }
+ }
+
+ // Step 9: Return the minimum of the two rotation counts
+ return Math.min(rotateTop, rotateBottom);
+ }
+}
+
\ No newline at end of file
From c049719798ec17654c46f53d9b9feac12ebc3a51 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 6 May 2025 09:04:47 +0530
Subject: [PATCH 12/58] Create
---
Java/Build Array from Permutation.java | 11 +++++++++++
Java/Domino and Tromino Tiling.java | 26 ++++++++++++++++++++++++++
2 files changed, 37 insertions(+)
create mode 100644 Java/Build Array from Permutation.java
create mode 100644 Java/Domino and Tromino Tiling.java
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/Domino and Tromino Tiling.java b/Java/Domino and Tromino Tiling.java
new file mode 100644
index 0000000000..971539563e
--- /dev/null
+++ b/Java/Domino and Tromino Tiling.java
@@ -0,0 +1,26 @@
+#include
+
+public class Solution {
+ public:
+ const int MOD = 1e9 + 7;
+ template
+ inline T mod_add(T a, T b, T m) { return (a % m + b % m) % m; }
+ vector dp;
+ int rec(int idx) {
+ if (idx == 0) return 1;
+ if (idx == 1) return 1;
+ if (idx == 2) return 2;
+ if (dp[idx] != -1) return dp[idx];
+ auto ans = (2LL * rec(idx - 1)) % MOD;
+ ans = mod_add(ans, rec(idx - 3), MOD);
+
+ return dp[idx] = ans;
+ }
+
+
+ int numTilings(int n) {
+ dp.assign(n + 1, -1);
+ return rec(n);
+ }
+ };
+
From 577aa59c07fe5a87e8e2e241f19ef7415bede7cb Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 7 May 2025 19:39:45 +0530
Subject: [PATCH 13/58] Changes
---
...Minimum time to reach the last Room I.java | 55 +++++++++++++++++++
1 file changed, 55 insertions(+)
create mode 100644 Java/Find Minimum time to reach the last Room I.java
diff --git a/Java/Find Minimum time to reach the last Room I.java b/Java/Find Minimum time to reach the last Room I.java
new file mode 100644
index 0000000000..b340e8113c
--- /dev/null
+++ b/Java/Find Minimum time to reach the last Room I.java
@@ -0,0 +1,55 @@
+class Solution {
+
+ private static final int INF = 0x3f3f3f3f;
+
+ public int minTimeToReach(int[][] moveTime) {
+ int n = moveTime.length, m = moveTime[0].length;
+ int[][] d = new int[n][m];
+ boolean[][] v = new boolean[n][m];
+ for (int i = 0; i < n; i++) {
+ Arrays.fill(d[i], INF);
+ }
+
+ int[][] dirs = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
+ d[0][0] = 0;
+ PriorityQueue q = new PriorityQueue<>();
+ q.offer(new State(0, 0, 0));
+
+ while (!q.isEmpty()) {
+ State s = q.poll();
+ if (v[s.x][s.y]) {
+ continue;
+ }
+ v[s.x][s.y] = true;
+ for (int[] dir : dirs) {
+ int nx = s.x + dir[0];
+ int ny = s.y + dir[1];
+ if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
+ continue;
+ }
+ int dist = Math.max(d[s.x][s.y], moveTime[nx][ny]) + 1;
+ if (d[nx][ny] > dist) {
+ d[nx][ny] = dist;
+ q.offer(new State(nx, ny, dist));
+ }
+ }
+ }
+ return d[n - 1][m - 1];
+ }
+
+ static class State implements Comparable {
+
+ int x, y, dis;
+
+ State(int x, int y, int dis) {
+ this.x = x;
+ this.y = y;
+ this.dis = dis;
+ }
+
+ @Override
+ public int compareTo(State other) {
+ return Integer.compare(this.dis, other.dis);
+ }
+ }
+}
\ No newline at end of file
From df698de2a7d481af4c8a4886505754ec13780c08 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 8 May 2025 21:15:16 +0530
Subject: [PATCH 14/58] Create
---
...inimum time to reach the last room II.java | 72 +++++++++++++++++++
1 file changed, 72 insertions(+)
create mode 100644 Java/Find Minimum time to reach the last room II.java
diff --git a/Java/Find Minimum time to reach the last room II.java b/Java/Find Minimum time to reach the last room II.java
new file mode 100644
index 0000000000..d04ab7516d
--- /dev/null
+++ b/Java/Find Minimum time to reach the last room II.java
@@ -0,0 +1,72 @@
+import java.util.*;
+
+public class Solution {
+
+ public int minTimeToReach(int[][] moveTime) {
+
+ int n = moveTime.length;
+
+ int m = moveTime[0].length;
+
+ int[][] bestTime = new int[n][m];
+
+ for (int[] row : bestTime) {
+
+ Arrays.fill(row, Integer.MAX_VALUE);
+
+ }
+
+ int[][] directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
+
+ PriorityQueue pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[0]));
+
+ pq.offer(new int[]{0, 0, 0, 0}); // time, i, j, nextMoveTime
+
+ while (!pq.isEmpty()) {
+
+ int[] curr = pq.poll();
+
+ int time = curr[0], i = curr[1], j = curr[2], nextMoveTime = curr[3];
+
+ if (time >= bestTime[i][j]) continue;
+
+ bestTime[i][j] = time;
+
+ if (i == n - 1 && j == m - 1) return time;
+
+ for (int[] d : directions) {
+
+ int x = i + d[0], y = j + d[1];
+
+ if (x >= 0 && x < n && y >= 0 && y < m) {
+
+ int wait = moveTime[x][y];
+
+ int futureMove = nextMoveTime == 1 ? 2 : 1;
+
+ int nextTime = wait > time ? wait + futureMove : time + futureMove;
+
+ if (i == 0 && j == 0 && wait <= time) {
+
+ nextTime = wait + futureMove;
+
+ }
+
+ if (nextTime < bestTime[x][y]) {
+
+ pq.offer(new int[]{nextTime, x, y, futureMove});
+
+ }
+
+ }
+
+ }
+
+ }
+
+ return -1; // Should never reach
+
+ }
+
+}
+
From c3fb29a2dc029a4e3ff2fce89e9d5dc318c08f9b Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Fri, 9 May 2025 12:45:07 +0530
Subject: [PATCH 15/58] Create Count Number of Balanced Permutation.java
---
Count Number of Balanced Permutation.java | 69 +++++++++++++++++++++++
1 file changed, 69 insertions(+)
create mode 100644 Count Number of Balanced Permutation.java
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];
+ }
+}
From 077559eae0d1f29ab6f24d07ab804ad656b2a066 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Sat, 10 May 2025 13:33:23 +0530
Subject: [PATCH 16/58] Minimum Equal sum of Two Array After Replacing
Zeroes.java
---
... of Two Arrays After Replacing Zeroes.java | 29 +++++++++++++++++++
1 file changed, 29 insertions(+)
create mode 100644 Minimum Equal Sum of Two Arrays After Replacing Zeroes.java
diff --git a/Minimum Equal Sum of Two Arrays After Replacing Zeroes.java b/Minimum Equal Sum of Two Arrays After Replacing Zeroes.java
new file mode 100644
index 0000000000..c10f60ffee
--- /dev/null
+++ b/Minimum Equal Sum of Two Arrays After Replacing Zeroes.java
@@ -0,0 +1,29 @@
+class Solution {
+
+ public long minSum(int[] nums1, int[] nums2) {
+ long sum1 = 0, sum2 = 0;
+ long zero1 = 0, zero2 = 0;
+
+ for (int i : nums1) {
+ sum1 += i;
+ if (i == 0) {
+ sum1++;
+ zero1++;
+ }
+ }
+
+ for (int i : nums2) {
+ sum2 += i;
+ if (i == 0) {
+ sum2++;
+ zero2++;
+ }
+ }
+
+ if ((zero1 == 0 && sum2 > sum1) || (zero2 == 0 && sum1 > sum2)) {
+ return -1;
+ }
+
+ return Math.max(sum1, sum2);
+ }
+}
\ No newline at end of file
From d3503e984295782f1508d759a2420f2296ec024d Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 12 May 2025 11:19:19 +0530
Subject: [PATCH 17/58] Create
---
Three Consecutive Odds.java | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 Three Consecutive Odds.java
diff --git a/Three Consecutive Odds.java b/Three Consecutive Odds.java
new file mode 100644
index 0000000000..f6077e692c
--- /dev/null
+++ b/Three Consecutive Odds.java
@@ -0,0 +1,14 @@
+class Solution {
+ public boolean threeConsecutiveOdds(int[] arr) {
+ // Loop through the array up to the third-to-last element
+ for (int i = 0; i < arr.length - 2; i++) {
+ // Check if the current element and the next two elements are all odd
+ int product=arr[i]*arr[i+1]*arr[i+2];
+ if(product% 2==1){
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
From 04552222744b7fc69880df42a681d3364e156068 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 13 May 2025 09:24:24 +0530
Subject: [PATCH 18/58] Create Length of the String After Transformation I.java
---
... of the String After Transformation I.java | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
create mode 100644 Length of the String After Transformation I.java
diff --git a/Length of the String After Transformation I.java b/Length of the String After Transformation I.java
new file mode 100644
index 0000000000..e088f80bd9
--- /dev/null
+++ b/Length of the String After Transformation I.java
@@ -0,0 +1,24 @@
+class Solution {
+ public int lengthAfterTransformations(String s, int t) {
+ int MOD = 1_000_000_007;
+ int[] v = new int[26];
+ for (char ch : s.toCharArray()) {
+ v[ch - 'a']++;
+ }
+ for (int i = 0; i < t; i++) {
+ int ele = v[25];
+ for (int j = 25; j > 0; j--) {
+ v[j] = v[j - 1];
+ }
+ v[0] = 0;
+ v[0] = (v[0] + ele) % MOD;
+ v[1] = (v[1] + ele) % MOD;
+ }
+ int sum = 0;
+ for (int i = 0; i < 26; i++) {
+ sum = (sum + v[i]) % MOD;
+ }
+ return sum;
+ }
+}
+
\ No newline at end of file
From 708339421d72113b827fc9c9cafa9eb28de24355 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 14 May 2025 09:47:14 +0530
Subject: [PATCH 19/58] Create Length oof the String After Transformation
II.java
---
Length of String After Transformation II.java | 50 +++++++++++++++++++
1 file changed, 50 insertions(+)
create mode 100644 Length of String After Transformation II.java
diff --git a/Length of String After Transformation II.java b/Length of String After Transformation II.java
new file mode 100644
index 0000000000..f2a1e660ce
--- /dev/null
+++ b/Length of String After Transformation II.java
@@ -0,0 +1,50 @@
+class Solution {
+ private static final int mod = 1000000007;
+
+ private long[][] multiplyMatrices(long[][] A, long[][] B) {
+ int rowsA = A.length, colsA = A[0].length, colsB = B[0].length;
+ long[][] result = new long[rowsA][colsB];
+ for (int i = 0; i < rowsA; i++) {
+ for (int j = 0; j < colsB; j++) {
+ long sum = 0;
+ for (int k = 0; k < colsA; k++) {
+ sum = (sum + (A[i][k] * B[k][j]) % mod) % mod;
+ }
+ result[i][j] = sum;
+ }
+ }
+ return result;
+ }
+
+ private long[][] powerMatrix(long[][] matrix, long exponent) {
+ int n = matrix.length;
+ long[][] result = new long[n][n];
+ for (int i = 0; i < n; i++) result[i][i] = 1;
+ while (exponent > 0) {
+ if ((exponent & 1) == 1) result = multiplyMatrices(result, matrix);
+ matrix = multiplyMatrices(matrix, matrix);
+ exponent >>= 1;
+ }
+ return result;
+ }
+
+ public int lengthAfterTransformations(String s, int t, List nums) {
+ long[][] transform = new long[26][26];
+ for (int i = 0; i < 26; i++) {
+ for (int shift = 0; shift < nums.get(i); shift++) {
+ transform[i][(i + 1 + shift) % 26]++;
+ }
+ }
+ transform = powerMatrix(transform, t);
+ long[][] freq = new long[1][26];
+ for (char ch : s.toCharArray()) {
+ freq[0][ch - 'a']++;
+ }
+ freq = multiplyMatrices(freq, transform);
+ long total = 0;
+ for (long cnt : freq[0]) {
+ total = (total + cnt) % mod;
+ }
+ return (int)total;
+ }
+}
\ No newline at end of file
From 9c03d5557cb396524e2d43d41e45d34c8913199e Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 15 May 2025 09:44:20 +0530
Subject: [PATCH 20/58] Create Longest Unequal Sequence I .java
---
...st Unequal Adjacent Sequence Groups I.java | 39 +++++++++++++++++++
1 file changed, 39 insertions(+)
create mode 100644 Longest Unequal Adjacent Sequence Groups I.java
diff --git a/Longest Unequal Adjacent Sequence Groups I.java b/Longest Unequal Adjacent Sequence Groups I.java
new file mode 100644
index 0000000000..e12b4df395
--- /dev/null
+++ b/Longest Unequal Adjacent Sequence Groups I.java
@@ -0,0 +1,39 @@
+public class class Solution {
+
+ public List getLongestSubsequence(String[] words, int[] groups) {
+ int n = words.length;
+ int[] dp = new int[n];
+ int[] prev = new int[n];
+ int maxLen = 1, endIndex = 0;
+
+ for (int i = 0; i < n; i++) {
+ dp[i] = 1;
+ prev[i] = -1;
+ }
+ for (int i = 1; i < n; i++) {
+ int bestLen = 1;
+ int bestPrev = -1;
+ for (int j = i - 1; j >= 0; j--) {
+ if (groups[i] != groups[j] && dp[j] + 1 > bestLen) {
+ bestLen = dp[j] + 1;
+ bestPrev = j;
+ }
+ }
+ dp[i] = bestLen;
+ prev[i] = bestPrev;
+ if (dp[i] > maxLen) {
+ maxLen = dp[i];
+ endIndex = i;
+ }
+ }
+
+ List res = new ArrayList<>();
+ for (int i = endIndex; i != -1; i = prev[i]) {
+ res.add(words[i]);
+ }
+ Collections.reverse(res);
+ return res;
+ }
+}
+
+
From c9f586df9f427541e7fe2c2fe71cc20f31be9edf Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Fri, 16 May 2025 19:55:36 +0530
Subject: [PATCH 21/58] Longest Unequal Adjacent Groups Sequence II.java
---
...t Unequal Adjacent Groups Sequence II.java | 51 +++++++++++++++++++
1 file changed, 51 insertions(+)
create mode 100644 Longest Unequal Adjacent Groups Sequence II.java
diff --git a/Longest Unequal Adjacent Groups Sequence II.java b/Longest Unequal Adjacent Groups Sequence II.java
new file mode 100644
index 0000000000..04f1d7a737
--- /dev/null
+++ b/Longest Unequal Adjacent Groups Sequence II.java
@@ -0,0 +1,51 @@
+class Solution {
+
+ public List getWordsInLongestSubsequence(
+ String[] words,
+ int[] groups
+ ) {
+ int n = groups.length;
+ int[] dp = new int[n];
+ int[] prev = new int[n];
+ Arrays.fill(dp, 1);
+ Arrays.fill(prev, -1);
+ int maxIndex = 0;
+ for (int i = 1; i < n; i++) {
+ for (int j = 0; j < i; j++) {
+ if (
+ check(words[i], words[j]) &&
+ dp[j] + 1 > dp[i] &&
+ groups[i] != groups[j]
+ ) {
+ dp[i] = dp[j] + 1;
+ prev[i] = j;
+ }
+ }
+ if (dp[i] > dp[maxIndex]) {
+ maxIndex = i;
+ }
+ }
+ List ans = new ArrayList<>();
+ for (int i = maxIndex; i >= 0; i = prev[i]) {
+ ans.add(words[i]);
+ }
+ Collections.reverse(ans);
+ return ans;
+ }
+
+ private boolean check(String s1, String s2) {
+ if (s1.length() != s2.length()) {
+ return false;
+ }
+ int diff = 0;
+ for (int i = 0; i < s1.length(); i++) {
+ if (s1.charAt(i) != s2.charAt(i)) {
+ if (++diff > 1) {
+ return false;
+ }
+ }
+ }
+ return diff == 1;
+ }
+}
+
From 2e89888d0733c94d0d5591ed19f72ef907719b5f Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Sat, 17 May 2025 08:37:09 +0530
Subject: [PATCH 22/58] Sort Colors
---
Sort Colors | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 Sort Colors
diff --git a/Sort Colors b/Sort Colors
new file mode 100644
index 0000000000..52765c5c79
--- /dev/null
+++ b/Sort Colors
@@ -0,0 +1,16 @@
+lass Solution {
+ public void sortColors(int[] nums) {
+ int [] count=new int[3];
+ for (int num:nums)
+ {
+ count[num]++;
+ }
+ int index=0;
+ for(int i=0;i<3;i++){
+ while(count[i]>0){
+ nums[index++]=i;
+ count[i]--;
+ }
+ }
+ }
+}
\ No newline at end of file
From 2993d4a7a52c782c99a9c5e4c531361d103b1a1e Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Sun, 18 May 2025 09:04:34 +0530
Subject: [PATCH 23/58] Create Painting the Grid with N different Colors.java
---
Painting the grid with n different colors | 66 +++++++++++++++++++++++
1 file changed, 66 insertions(+)
create mode 100644 Painting the grid with n different colors
diff --git a/Painting the grid with n different colors b/Painting the grid with n different colors
new file mode 100644
index 0000000000..1826946429
--- /dev/null
+++ b/Painting the grid with n different colors
@@ -0,0 +1,66 @@
+class Solution {
+
+ // update RGB combination as 0x7 for improvement.
+
+ private static final int R = 1;
+ private static final int G = 2;
+ private static final int B = 4;
+ private static final int mod = (int)1e9+7;
+ public int colorTheGrid(int m, int n) {
+
+ //compute overall number of possible permutations for a single column
+ int count = 3;
+ for(int i = 0; i < m - 1; i++)
+ count *= 2;
+
+ long[] perms = new long[count];
+ Queue que = new LinkedList<>();
+
+ que.offer(R);
+ que.offer(G);
+ que.offer(B);
+
+ //fill in all possible permuatations using Queue
+ int index = count;
+
+ while(!que.isEmpty()){
+ int color = que.poll();
+ if( color >= 1 << 3 * (m-1) ) //m - length permutation
+ perms[--index] = color;
+ else{
+
+ int nextcolors = 0x7^ (color & 0x7);
+ while(nextcolors > 0){
+ int nextcolor = nextcolors &(-nextcolors);
+ que.add((color << 3) + nextcolor);
+ nextcolors &= (nextcolors - 1);
+ }
+ }
+ }
+
+ int[] cur = new int[count];
+ Arrays.fill(cur, 1);
+
+ //iterate by columns from left to right,
+ //memorizing score count for every permutation in current column
+ for(int col = 0; col < n-1; col++){
+
+ int[] next = new int[count];
+
+ for(int i = 0; i < count; i++){
+ for(int j = 0; j < count; j++){
+ if((perms[i] & perms[j]) == 0) // both are OK to each other
+ next[j] = (next[j] + cur[i])%mod;
+ }
+ }
+ cur = next;
+ }
+
+ //accumulate all scores for permuttions as per last column
+ int result = 0;
+ for(int i = 0; i < count; i++)
+ result = (result + cur[i]) % mod;
+
+ return result;
+ }
+}
\ No newline at end of file
From 1dcec1060b6b50778e55fddb712471d7fc1bbdda Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 19 May 2025 19:06:54 +0530
Subject: [PATCH 24/58] Create Type of Triangle.java
---
Type of Triangle.java | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 Type of Triangle.java
diff --git a/Type of Triangle.java b/Type of Triangle.java
new file mode 100644
index 0000000000..84cebe1a95
--- /dev/null
+++ b/Type of Triangle.java
@@ -0,0 +1,15 @@
+class Solution {
+
+ public String triangleType(int[] nums) {
+ Arrays.sort(nums);
+ if (nums[0] + nums[1] <= nums[2]) {
+ return "none";
+ } else if (nums[0] == nums[2]) {
+ return "equilateral";
+ } else if (nums[0] == nums[1] || nums[1] == nums[2]) {
+ return "isosceles";
+ } else {
+ return "scalene";
+ }
+ }
+}
From ec187056e11b9c2350ce0cb76423d88e1e02eebc Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 20 May 2025 19:46:28 +0530
Subject: [PATCH 25/58] Zero Array Transformation I
---
Zero Array Transformation I.java | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
create mode 100644 Zero Array Transformation I.java
diff --git a/Zero Array Transformation I.java b/Zero Array Transformation I.java
new file mode 100644
index 0000000000..877d99dc55
--- /dev/null
+++ b/Zero Array Transformation I.java
@@ -0,0 +1,24 @@
+class Solution {
+
+ public boolean isZeroArray(int[] nums, int[][] queries) {
+ int[] deltaArray = new int[nums.length + 1];
+ for (int[] query : queries) {
+ int left = query[0];
+ int right = query[1];
+ deltaArray[left] += 1;
+ deltaArray[right + 1] -= 1;
+ }
+ int[] operationCounts = new int[deltaArray.length];
+ int currentOperations = 0;
+ for (int i = 0; i < deltaArray.length; i++) {
+ currentOperations += deltaArray[i];
+ operationCounts[i] = currentOperations;
+ }
+ for (int i = 0; i < nums.length; i++) {
+ if (operationCounts[i] < nums[i]) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
\ No newline at end of file
From f0cff592bb0666ebaba6c63f1d6ededf2e4e0fa1 Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 21 May 2025 19:37:45 +0530
Subject: [PATCH 26/58] Set Zeroes Matrix
---
Set Zeroes Matrix.java | 0
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 Set Zeroes Matrix.java
diff --git a/Set Zeroes Matrix.java b/Set Zeroes Matrix.java
new file mode 100644
index 0000000000..e69de29bb2
From ea7840c81440f599c76e1b854d76da3f3c467bbc Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 21 May 2025 19:38:30 +0530
Subject: [PATCH 27/58] Create
---
Set Zeroes Matrix.java | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/Set Zeroes Matrix.java b/Set Zeroes Matrix.java
index e69de29bb2..fee5785605 100644
--- a/Set Zeroes Matrix.java
+++ b/Set Zeroes Matrix.java
@@ -0,0 +1,25 @@
+class Solution {
+ public void setZeroes(int[][] matrix) {
+ int n = matrix.length;
+ int m = matrix[0].length;
+ boolean[] row = new boolean[n];
+ boolean[] col = new boolean[m];
+
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < m; j++) {
+ if (matrix[i][j] == 0) {
+ row[i] = true;
+ col[j] = true;
+ }
+ }
+ }
+
+ for (int i = 0; i < n; i++) {
+ for (int j = 0; j < m; j++) {
+ if (row[i] || col[j]) {
+ matrix[i][j] = 0;
+ }
+ }
+ }
+ }
+}
\ No newline at end of file
From 4029ede9bc19f21056f01931de136568624d2fd3 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 22 May 2025 20:29:27 +0530
Subject: [PATCH 28/58] Create Zero Array Transformation.java
---
Java/Zero Array Transformation.java | 32 +++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 Java/Zero Array Transformation.java
diff --git a/Java/Zero Array Transformation.java b/Java/Zero Array Transformation.java
new file mode 100644
index 0000000000..76e5ea4653
--- /dev/null
+++ b/Java/Zero Array Transformation.java
@@ -0,0 +1,32 @@
+class Solution {
+ public static int maxRemoval(int[] nums, int[][] queries) {
+ int n = nums.length, q = queries.length;
+ List> qEnd = new ArrayList<>();
+ for (int i = 0; i < n; i++) qEnd.add(new ArrayList<>());
+ for (int[] query : queries) {
+ qEnd.get(query[0]).add(query[1]);
+ }
+
+ PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder());
+ int[] cntQ = new int[n + 1];
+ int dec = 0;
+
+ for (int i = 0; i < n; i++) {
+ dec += cntQ[i];
+ for (int end : qEnd.get(i)) {
+ pq.offer(end);
+ }
+
+ int x = nums[i];
+ while (x > dec && !pq.isEmpty() && pq.peek() >= i) {
+ int k = pq.poll();
+ cntQ[k + 1]--;
+ dec++;
+ }
+
+ if (x > dec) return -1;
+ }
+
+ return pq.size();
+ }
+}
From 761d45fd41f7b2833267d1a42d6c40928085164d Mon Sep 17 00:00:00 2001
From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 22 May 2025 21:10:25 +0530
Subject: [PATCH 29/58] Zero Array Transformation III
---
Zero Array Transformation III.java | 32 ++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+)
create mode 100644 Zero Array Transformation III.java
diff --git a/Zero Array Transformation III.java b/Zero Array Transformation III.java
new file mode 100644
index 0000000000..72ecfedf0e
--- /dev/null
+++ b/Zero Array Transformation III.java
@@ -0,0 +1,32 @@
+class Solution {
+ public static int maxRemoval(int[] nums, int[][] queries) {
+ int n = nums.length, q = queries.length;
+ List> qEnd = new ArrayList<>();
+ for (int i = 0; i < n; i++) qEnd.add(new ArrayList<>());
+ for (int[] query : queries) {
+ qEnd.get(query[0]).add(query[1]);
+ }
+
+ PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder());
+ int[] cntQ = new int[n + 1];
+ int dec = 0;
+
+ for (int i = 0; i < n; i++) {
+ dec += cntQ[i];
+ for (int end : qEnd.get(i)) {
+ pq.offer(end);
+ }
+
+ int x = nums[i];
+ while (x > dec && !pq.isEmpty() && pq.peek() >= i) {
+ int k = pq.poll();
+ cntQ[k + 1]--;
+ dec++;
+ }
+
+ if (x > dec) return -1;
+ }
+
+ return pq.size();
+ }
+}
\ No newline at end of file
From 625e1764a687df40dc68f72f4c9f982b3f70df2f Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 22 May 2025 21:21:03 +0530
Subject: [PATCH 30/58] Create Valid parenthesis.java
---
Valid parenthesis.java | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
create mode 100644 Valid parenthesis.java
diff --git a/Valid parenthesis.java b/Valid parenthesis.java
new file mode 100644
index 0000000000..ff46fbc7af
--- /dev/null
+++ b/Valid parenthesis.java
@@ -0,0 +1,22 @@
+bool isValid(char* s) {
+ int n = strlen(s);
+ char stack[n];
+ int top = -1;
+
+ for (int i = 0; i < n; i++) {
+ if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
+ stack[++top] = s[i];
+ } else {
+ if (top == -1) {
+ return false;
+ }
+ char last = stack[top];
+ if ((last == '(' && s[i] == ')') || (last == '{' && s[i] == '}') || (last == '[' && s[i] == ']')) {
+ top--;
+ } else {
+ return false;
+ }
+ }
+ }
+ return top == -1;
+}
From 8e676f9d5aeab74facf60bc0d25b24939b73869f Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Fri, 23 May 2025 19:07:34 +0530
Subject: [PATCH 31/58] Create Reverse Integer.cpp
---
Reverse Integer.cpp | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 Reverse Integer.cpp
diff --git a/Reverse Integer.cpp b/Reverse Integer.cpp
new file mode 100644
index 0000000000..e24130d942
--- /dev/null
+++ b/Reverse Integer.cpp
@@ -0,0 +1,18 @@
+class Solution {
+public:
+ int reverse(int x) {
+ int ans = 0; // Initialize the reversed number to 0
+ while (x != 0) {
+ int digit = x % 10; // Get the last digit of x
+
+ // Check for overflow/underflow before updating ans
+ if ((ans > INT_MAX / 10) || (ans < INT_MIN / 10)) {
+ return 0; // Return 0 if reversing x would cause overflow/underflow
+ }
+
+ ans = ans * 10 + digit; // Append the digit to the reversed number
+ x = x / 10; // Remove the last digit from x
+ }
+ return ans; // Return the reversed number
+ }
+};
From a0f7c65631cabdeb86de7da16547c0f4f9a26114 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sat, 24 May 2025 12:57:36 +0530
Subject: [PATCH 32/58] Create Find Words Containing Character.java
---
Find Words Containing Character.java | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 Find Words Containing Character.java
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
Date: Sun, 25 May 2025 16:43:10 +0530
Subject: [PATCH 33/58] Create Longest palindrome by concatenating two
words.java
---
Longest palindrome by concatenating two words | 31 +++++++++++++++++++
1 file changed, 31 insertions(+)
create mode 100644 Longest palindrome by concatenating two words
diff --git a/Longest palindrome by concatenating two words b/Longest palindrome by concatenating two words
new file mode 100644
index 0000000000..0b89c378c4
--- /dev/null
+++ b/Longest palindrome by concatenating two words
@@ -0,0 +1,31 @@
+import java.util.*;
+
+class Solution {
+ public int longestPalindrome(String[] words) {
+ Map map = new HashMap<>();
+ int count = 0;
+ boolean hasCentralWord = false;
+
+ for (String word : words)
+ map.put(word, map.getOrDefault(word, 0) + 1);
+
+ for (String word : map.keySet()) {
+ int freq = map.get(word);
+ if (freq == 0) continue;
+
+ String reversed = new StringBuilder(word).reverse().toString();
+ if (word.equals(reversed)) {
+ count += (freq / 2) * 4;
+ if (freq % 2 == 1) hasCentralWord = true;
+ } else if (map.containsKey(reversed)) {
+ int pairs = Math.min(freq, map.get(reversed));
+ count += pairs * 4;
+ map.put(reversed, 0);
+ }
+ map.put(word, 0);
+ }
+
+ if (hasCentralWord) count += 2;
+ return count;
+ }
+}
From 17066bcdc28ca33f99f5ae9c10a2be1fa615a689 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 26 May 2025 19:27:09 +0530
Subject: [PATCH 34/58] Create Add Two Numbers.java
---
Add Two Numbers.java | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)
create mode 100644 Add Two Numbers.java
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;
+ }
+}
From 037cd67ce86ce2877db1fedc5f00eace57d29a1d Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 27 May 2025 08:46:11 +0530
Subject: [PATCH 35/58] Create Divisible And Non-divisible Sum Differences.java
---
Divisible And Non-divisible Sum Differences.java | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 Divisible And Non-divisible Sum Differences.java
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;
+ }
+}
From c6a57ba3bd12c248978a297c06392a73e45a9e8c Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 28 May 2025 08:35:08 +0530
Subject: [PATCH 36/58] Create Reverse Integer . java
---
Java/Reverse Integer . java | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 Java/Reverse Integer . java
diff --git a/Java/Reverse Integer . java b/Java/Reverse Integer . java
new file mode 100644
index 0000000000..da0fe250b1
--- /dev/null
+++ b/Java/Reverse Integer . java
@@ -0,0 +1,16 @@
+class Solution {
+ public int reverse(int x) {
+ int ans = 0;
+ while(x != 0){
+ int rem = x % 10;
+ if(ans < Integer.MIN_VALUE/10 || ans > Integer.MAX_VALUE/10)
+ return 0;
+ ans = (ans*10) + rem;
+ x = x/10;
+
+ }
+
+ return ans;
+
+ }
+}
From 4cfbb7adc251f168af093f1797606a1f05860891 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 29 May 2025 08:44:11 +0530
Subject: [PATCH 37/58] Create Palindrome Number.cpp
---
C++/Palindrome Number.cpp | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
create mode 100644 C++/Palindrome Number.cpp
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 || revNum
Date: Fri, 30 May 2025 08:53:42 +0530
Subject: [PATCH 38/58] Create Longest Palindromic Substring.java
---
Java/Longest Palindromic Substring.java | 36 +++++++++++++++++++++++++
1 file changed, 36 insertions(+)
create mode 100644 Java/Longest Palindromic Substring.java
diff --git a/Java/Longest Palindromic Substring.java b/Java/Longest Palindromic Substring.java
new file mode 100644
index 0000000000..a7b19c40bd
--- /dev/null
+++ b/Java/Longest Palindromic Substring.java
@@ -0,0 +1,36 @@
+public class Solution {
+ public String longestPalindrome(String s) {
+ if (s.length() <= 1) {
+ return s;
+ }
+
+ int maxLen = 1;
+ String maxStr = s.substring(0, 1);
+
+ for (int i = 0; i < s.length(); i++) {
+ for (int j = i + maxLen; j <= s.length(); j++) {
+ if (j - i > maxLen && isPalindrome(s.substring(i, j))) {
+ maxLen = j - i;
+ maxStr = s.substring(i, j);
+ }
+ }
+ }
+
+ return maxStr;
+ }
+
+ private boolean isPalindrome(String str) {
+ int left = 0;
+ int right = str.length() - 1;
+
+ while (left < right) {
+ if (str.charAt(left) != str.charAt(right)) {
+ return false;
+ }
+ left++;
+ right--;
+ }
+
+ return true;
+ }
+}
From 973f390519b4c1fd6cbeae66b649d5b83a3d4a51 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sat, 31 May 2025 10:42:01 +0530
Subject: [PATCH 39/58] Create Snakes And Ladders.java
---
Java/Snakes And Ladders.java | 48 ++++++++++++++++++++++++++++++++++++
1 file changed, 48 insertions(+)
create mode 100644 Java/Snakes And Ladders.java
diff --git a/Java/Snakes And Ladders.java b/Java/Snakes And Ladders.java
new file mode 100644
index 0000000000..7910770562
--- /dev/null
+++ b/Java/Snakes And Ladders.java
@@ -0,0 +1,48 @@
+class Solution {
+ public int snakesAndLadders(int[][] board) {
+ int n = board.length;
+ boolean[] visited = new boolean[n * n + 1];
+ Queue queue = new LinkedList<>();
+ queue.offer(new int[]{1, 0}); // {square number, number of moves}
+ visited[1] = true;
+
+ while (!queue.isEmpty()) {
+ int[] curr = queue.poll();
+ int square = curr[0];
+ int moves = curr[1];
+
+ for (int i = 1; i <= 6; i++) {
+ int next = square + i;
+ if (next > n * n) continue;
+
+ int[] pos = getCoordinates(next, n);
+ int r = pos[0], c = pos[1];
+
+ if (board[r][c] != -1) {
+ next = board[r][c];
+ }
+
+ if (next == n * n) {
+ return moves + 1;
+ }
+
+ if (!visited[next]) {
+ visited[next] = true;
+ queue.offer(new int[]{next, moves + 1});
+ }
+ }
+ }
+
+ return -1; // unreachable
+ }
+
+ // Converts 1-based square number to (row, col) in board
+ private int[] getCoordinates(int num, int n) {
+ int r = (n - 1) - (num - 1) / n;
+ int c = (num - 1) % n;
+ if (((n - 1 - r) % 2) == 1) {
+ c = n - 1 - c;
+ }
+ return new int[]{r, c};
+ }
+}
From 875a93cf677b27e1d555b508ad4c2aefeeacec6b Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sun, 1 Jun 2025 09:29:37 +0530
Subject: [PATCH 40/58] Create Distribute Candies Among Children II .java
---
Java/Distribute Candies Among Children II .java | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 Java/Distribute Candies Among Children II .java
diff --git a/Java/Distribute Candies Among Children II .java b/Java/Distribute Candies Among Children II .java
new file mode 100644
index 0000000000..2cd3b4f794
--- /dev/null
+++ b/Java/Distribute Candies Among Children II .java
@@ -0,0 +1,14 @@
+class Solution {
+ public long distributeCandies(int n, int limit) {
+ java.util.function.LongUnaryOperator C2 = x -> (x >= 2) ? (x * (x - 1) / 2) : 0L;
+ long N = n, L = limit;
+ long total = (N + 2) * (N + 1) / 2;
+ long x1 = N - L + 1;
+ long t1 = C2.applyAsLong(x1);
+ long x2 = N - 2 * L;
+ long t2 = C2.applyAsLong(x2);
+ long x3 = N - 3 * L - 1;
+ long t3 = C2.applyAsLong(x3);
+ return total - 3 * t1 + 3 * t2 - t3;
+ }
+}
From 4c13f67e5cd4e59296564c8f561f732b945fdc69 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 2 Jun 2025 09:36:47 +0530
Subject: [PATCH 41/58] Create Candy.java
---
Java/Candy.java | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
create mode 100644 Java/Candy.java
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;
+ }
+}
From f48c37a2c280f80c7f217a9a0cf3340e6ad45a55 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 2 Jun 2025 09:38:54 +0530
Subject: [PATCH 42/58] Create Zigzag Conversion
---
C++/Zigzag Conversion | 33 +++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
create mode 100644 C++/Zigzag Conversion
diff --git a/C++/Zigzag Conversion b/C++/Zigzag Conversion
new file mode 100644
index 0000000000..4b4d073fa8
--- /dev/null
+++ b/C++/Zigzag Conversion
@@ -0,0 +1,33 @@
+
+
+class Solution {
+public:
+
+ string convert(string s, int numRows) {
+
+ if(numRows <= 1) return s;
+
+ vectorv(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;
+
+ }
+};
From 6c4c7544fb152e37275bef72acbb0a8b10bfb897 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 3 Jun 2025 09:00:04 +0530
Subject: [PATCH 43/58] Create Divide Two Integers.java
---
Java/Divide Two Integers.java | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
create mode 100644 Java/Divide Two Integers.java
diff --git a/Java/Divide Two Integers.java b/Java/Divide Two Integers.java
new file mode 100644
index 0000000000..dc074a186b
--- /dev/null
+++ b/Java/Divide Two Integers.java
@@ -0,0 +1,25 @@
+class Solution {
+ public int divide(int dividend, int divisor) {
+ if (divisor == 1) {
+ return dividend;
+ }
+ if (dividend == Integer.MIN_VALUE && divisor == -1) {
+ return Integer.MAX_VALUE;
+ }
+ boolean sign = (dividend> 0 && divisor > 0) || (dividend< 0 && divisor< 0);
+ dividend= dividend > 0 ? -dividend : dividend;
+ divisor = divisor > 0 ? -divisor : divisor;
+ int ans = 0;
+ while (dividend <= divisor) {
+ int x = divisor;
+ int cnt = 1;
+ while (x >= (Integer.MIN_VALUE >> 1) && dividend <= (x << 1)) {
+ x <<= 1;
+ cnt <<= 1;
+ }
+ ans += cnt;
+ dividend-= x;
+ }
+ return sign ? ans : -ans;
+ }
+}
From 56a678c22972144c126609abcb9d42f0a4513987 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 4 Jun 2025 09:39:08 +0530
Subject: [PATCH 44/58] Create Find the Lexicographically Largest string from
the box .java
---
...raphically Largest string from the box .java | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 Java/Find the Lexicographically Largest string from the box .java
diff --git a/Java/Find the Lexicographically Largest string from the box .java b/Java/Find the Lexicographically Largest string from the box .java
new file mode 100644
index 0000000000..24e571c0f6
--- /dev/null
+++ b/Java/Find the Lexicographically Largest string from the box .java
@@ -0,0 +1,17 @@
+class Solution {
+
+ public String answerString(String word, int numFriends) {
+ if (numFriends == 1) {
+ return word;
+ }
+ int n = word.length();
+ String res = "";
+ for (int i = 0; i < n; i++) {
+ String s = word.substring(i, Math.min(i + n - numFriends + 1, n));
+ if (res.compareTo(s) <= 0) {
+ res = s;
+ }
+ }
+ return res;
+ }
+}
From 5db6813ec0772d9e2048e9380f8de4ce3d2fcaaf Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 5 Jun 2025 08:46:54 +0530
Subject: [PATCH 45/58] Create Roman To Integer.cpp
---
C++/Roman To Integer.cpp | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
create mode 100644 C++/Roman To Integer.cpp
diff --git a/C++/Roman To Integer.cpp b/C++/Roman To Integer.cpp
new file mode 100644
index 0000000000..2ff259aab2
--- /dev/null
+++ b/C++/Roman To Integer.cpp
@@ -0,0 +1,27 @@
+class Solution {
+public:
+ int char2num(char a) {
+ switch (a) {
+ case 'I': return 1;
+ case 'V': return 5;
+ case 'X': return 10;
+ case 'L': return 50;
+ case 'C': return 100;
+ case 'D': return 500;
+ case 'M': return 1000;
+ default: return 0;
+ }
+ }
+
+ int romanToInt(string s) {
+ int result = 0;
+ for (int i = 0; i < s.length(); i++) {
+ if (i + 1 < s.length() && char2num(s[i]) < char2num(s[i + 1])) {
+ result -= char2num(s[i]);
+ } else {
+ result += char2num(s[i]);
+ }
+ }
+ return result;
+ }
+};
From 5d77cdb464cfe1c2b98d16fc08a97b3bb92f5b17 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Fri, 6 Jun 2025 10:43:05 +0530
Subject: [PATCH 46/58] Create Using a Robot to Print the Lexicographically the
Smallest String .java
---
...exicographically the Smallest String .java | 43 +++++++++++++++++++
1 file changed, 43 insertions(+)
create mode 100644 Java/Using a Robot to Print the Lexicographically the Smallest String .java
diff --git a/Java/Using a Robot to Print the Lexicographically the Smallest String .java b/Java/Using a Robot to Print the Lexicographically the Smallest String .java
new file mode 100644
index 0000000000..f57a17ef37
--- /dev/null
+++ b/Java/Using a Robot to Print the Lexicographically the Smallest String .java
@@ -0,0 +1,43 @@
+class Solution
+{
+ public String robotWithString(String s)
+ {
+ int n = s.length();
+
+ // Step 1: Initialize min_suffix array
+ char[] minSuffix = new char[n];
+ minSuffix[n - 1] = s.charAt(n - 1);
+
+ // Step 2: Fill min_suffix from right to left
+ for (int i = n - 2; i >= 0; i--)
+ {
+ minSuffix[i] = (char)Math.min(s.charAt(i), minSuffix[i + 1]);
+ }
+
+ // Step 3: Initialize result and stack
+ Deque stack = new ArrayDeque<>();
+ StringBuilder result = new StringBuilder();
+ int i = 0;
+
+ // Step 4: Process characters from s
+ while (i < n)
+ {
+ stack.push(s.charAt(i++)); // Push to t
+
+ // Step 4 (cont.): Pop from t to result if top is safe
+ while (!stack.isEmpty() && stack.peek() <= minSuffix[i == n ? n - 1 : i])
+ {
+ result.append(stack.pop());
+ }
+ }
+
+ // Step 5: Clean remaining stack
+ while (!stack.isEmpty())
+ {
+ result.append(stack.pop());
+ }
+
+ // Step 6: Return result
+ return result.toString();
+ }
+}
From 3f811fea346737a9af800fbf9777341ceb08f6f1 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sat, 7 Jun 2025 10:08:44 +0530
Subject: [PATCH 47/58] Create Integer To Roman.java
---
Java/Integer To Roman.java | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 Java/Integer To Roman.java
diff --git a/Java/Integer To Roman.java b/Java/Integer To Roman.java
new file mode 100644
index 0000000000..05718dc1bf
--- /dev/null
+++ b/Java/Integer To Roman.java
@@ -0,0 +1,13 @@
+class Solution {
+ public String intToRoman(int num) {
+ String Roman = "";
+ int[][] storeIntRoman = {{1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}};
+ for (int i = 0; i < storeIntRoman.length; i++) {
+ while (num >= storeIntRoman[i][0]) {
+ Roman += storeIntRoman[i][1];
+ num -= storeIntRoman[i][0];
+ }
+ }
+ return Roman;
+ }
+}
From 8c4e53ef73c777b019c4aed526e2b49aab7662a2 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sun, 8 Jun 2025 10:55:09 +0530
Subject: [PATCH 48/58] Create Lexicographical Numbers
---
Python3/Lexicographical Numbers | 9 +++++++++
1 file changed, 9 insertions(+)
create mode 100644 Python3/Lexicographical Numbers
diff --git a/Python3/Lexicographical Numbers b/Python3/Lexicographical Numbers
new file mode 100644
index 0000000000..234f0d2d1f
--- /dev/null
+++ b/Python3/Lexicographical Numbers
@@ -0,0 +1,9 @@
+class Solution:
+ def lexicalOrder(self, n: int) -> List[int]:
+ l=[] # take list
+ for i in range (1, n+1) :
+ l.append(str(i)) # store all n numbers as string
+ l.sort() # list is sorted to get lexicographical
+ for i in range(n):
+ l[i]=int(l[i]) # convert all strings into integers
+ return l
From 4a87008a7dd1dd140ffb371530a64d53434764ad Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Mon, 9 Jun 2025 10:03:16 +0530
Subject: [PATCH 49/58] Create Kth Smallest in Lexicograpical Order
---
Java/Kth Smallest in Lexicograpical Order | 34 +++++++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 Java/Kth Smallest in Lexicograpical Order
diff --git a/Java/Kth Smallest in Lexicograpical Order b/Java/Kth Smallest in Lexicograpical Order
new file mode 100644
index 0000000000..0226dbe777
--- /dev/null
+++ b/Java/Kth Smallest in Lexicograpical Order
@@ -0,0 +1,34 @@
+class Solution {
+
+ public int findKthNumber(int n, int k) {
+ int curr = 1;
+ k--;
+
+ while (k > 0) {
+ int step = countSteps(n, curr, curr + 1);
+ // If the steps are less than or equal to k, we skip this prefix's subtree
+ if (step <= k) {
+ // Move to the next prefix and decrease k by the number of steps we skip
+ curr++;
+ k -= step;
+ } else {
+ // Move to the next level of the tree and decrement k by 1
+ curr *= 10;
+ k--;
+ }
+ }
+
+ return curr;
+ }
+
+ // To count how many numbers exist between prefix1 and prefix2
+ private int countSteps(int n, long prefix1, long prefix2) {
+ int steps = 0;
+ while (prefix1 <= n) {
+ steps += Math.min(n + 1, prefix2) - prefix1;
+ prefix1 *= 10;
+ prefix2 *= 10;
+ }
+ return steps;
+ }
+}
From b63b8bfc566f47038598d6494ed14ca43ec562fd Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 10 Jun 2025 08:59:38 +0530
Subject: [PATCH 50/58] Create Maximum Difference Between Even and Odd
frequency I.java
---
...erence Between Even and Odd frequency I.java | 17 +++++++++++++++++
1 file changed, 17 insertions(+)
create mode 100644 Java/Maximum Difference Between Even and Odd frequency I.java
diff --git a/Java/Maximum Difference Between Even and Odd frequency I.java b/Java/Maximum Difference Between Even and Odd frequency I.java
new file mode 100644
index 0000000000..a53ef150f5
--- /dev/null
+++ b/Java/Maximum Difference Between Even and Odd frequency I.java
@@ -0,0 +1,17 @@
+class Solution {
+ public int maxDifference(String s) {
+ Map freq = new HashMap<>();
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ freq.put(c, freq.getOrDefault(c, 0) + 1);
+ }
+ int maxOdd = -1;
+ int minEven = Integer.MAX_VALUE;
+ for (int f : freq.values()) {
+ if ((f & 1) == 1) maxOdd = Math.max(maxOdd, f);
+ else minEven = Math.min(minEven, f);
+ }
+ if (maxOdd == -1 || minEven == Integer.MAX_VALUE) return -1;
+ return maxOdd - minEven;
+ }
+}
From 5a5b04f7f85cbaf9ecafe3371b2b3f70ca1d7180 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 11 Jun 2025 19:40:29 +0530
Subject: [PATCH 51/58] Create Maximum Difference Between Even and Odd
Frequency II .java
---
...ce Between Even and Odd Frequency II .java | 48 +++++++++++++++++++
1 file changed, 48 insertions(+)
create mode 100644 Java/Maximum Difference Between Even and Odd Frequency II .java
diff --git a/Java/Maximum Difference Between Even and Odd Frequency II .java b/Java/Maximum Difference Between Even and Odd Frequency II .java
new file mode 100644
index 0000000000..13ffa173b2
--- /dev/null
+++ b/Java/Maximum Difference Between Even and Odd Frequency II .java
@@ -0,0 +1,48 @@
+class Solution {
+
+ public int maxDifference(String s, int k) {
+ int n = s.length();
+ int ans = Integer.MIN_VALUE;
+ for (char a = '0'; a <= '4'; ++a) {
+ for (char b = '0'; b <= '4'; ++b) {
+ if (a == b) {
+ continue;
+ }
+ int[] best = new int[4];
+ Arrays.fill(best, Integer.MAX_VALUE);
+ int cnt_a = 0, cnt_b = 0;
+ int prev_a = 0, prev_b = 0;
+ int left = -1;
+
+ for (int right = 0; right < n; ++right) {
+ cnt_a += (s.charAt(right) == a) ? 1 : 0;
+ cnt_b += (s.charAt(right) == b) ? 1 : 0;
+
+ while (right - left >= k && cnt_b - prev_b >= 2) {
+ int left_status = getStatus(prev_a, prev_b);
+ best[left_status] = Math.min(
+ best[left_status],
+ prev_a - prev_b
+ );
+ ++left;
+ prev_a += (s.charAt(left) == a) ? 1 : 0;
+ prev_b += (s.charAt(left) == b) ? 1 : 0;
+ }
+
+ int right_status = getStatus(cnt_a, cnt_b);
+ if (best[right_status ^ 0b10] != Integer.MAX_VALUE) {
+ ans = Math.max(
+ ans,
+ cnt_a - cnt_b - best[right_status ^ 0b10]
+ );
+ }
+ }
+ }
+ }
+ return ans;
+ }
+
+ private int getStatus(int cnt_a, int cnt_b) {
+ return ((cnt_a & 1) << 1) | (cnt_b & 1);
+ }
+}
From 73555fdf625d4a128e6036ef79ced712eb6b9223 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 12 Jun 2025 08:22:07 +0530
Subject: [PATCH 52/58] Create Maximum Difference between adjacent elements in
a Circular Array.java
---
... between adjacent elements in a Circular Array.java | 10 ++++++++++
1 file changed, 10 insertions(+)
create mode 100644 Java/Maximum Difference between adjacent elements in a Circular Array.java
diff --git a/Java/Maximum Difference between adjacent elements in a Circular Array.java b/Java/Maximum Difference between adjacent elements in a Circular Array.java
new file mode 100644
index 0000000000..397823b7bb
--- /dev/null
+++ b/Java/Maximum Difference between adjacent elements in a Circular Array.java
@@ -0,0 +1,10 @@
+class Solution {
+ public int maxAdjacentDistance(int[] nums) {
+ int n = nums.length;
+ int res = Math.abs(nums[0] - nums[n - 1]);
+ for (int i = 0; i < n - 1; ++i) {
+ res = Math.max(res, Math.abs(nums[i] - nums[i + 1]));
+ }
+ return res;
+ }
+}
From 40a01be1834fb217e5f9e0ea14f51e43c71596bc Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Fri, 13 Jun 2025 07:27:33 +0530
Subject: [PATCH 53/58] Create Minimize the Maximum Difference of Pairs.java
---
...imize the Maximum Difference of Pairs.java | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 Java/Minimize the Maximum Difference of Pairs.java
diff --git a/Java/Minimize the Maximum Difference of Pairs.java b/Java/Minimize the Maximum Difference of Pairs.java
new file mode 100644
index 0000000000..21c5e56d6f
--- /dev/null
+++ b/Java/Minimize the Maximum Difference of Pairs.java
@@ -0,0 +1,34 @@
+class Solution {
+ // Find the number of valid pairs by greedy approach
+ private int countValidPairs(int[] nums, int threshold) {
+ int index = 0, count = 0;
+ while (index < nums.length - 1) {
+ // If a valid pair is found, skip both numbers.
+ if (nums[index + 1] - nums[index] <= threshold) {
+ count++;
+ index++;
+ }
+ index++;
+ }
+ return count;
+ }
+
+ public int minimizeMax(int[] nums, int p) {
+ Arrays.sort(nums);
+ int n = nums.length;
+ int left = 0, right = nums[n - 1] - nums[0];
+
+ while (left < right) {
+ int mid = left + (right - left) / 2;
+
+ // If there are enough pairs, look for a smaller threshold.
+ // Otherwise, look for a larger threshold.
+ if (countValidPairs(nums, mid) >= p) {
+ right = mid;
+ } else {
+ left = mid + 1;
+ }
+ }
+ return left;
+ }
+}
From 6be2239f2643c7fa51fb6af15d8fc45a4400de83 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sat, 14 Jun 2025 12:28:39 +0530
Subject: [PATCH 54/58] Create Maximum Difference By Remapping a Digit.java
---
.../Maximum Difference By Remapping a Digit.java | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
create mode 100644 Java/Maximum Difference By Remapping a Digit.java
diff --git a/Java/Maximum Difference By Remapping a Digit.java b/Java/Maximum Difference By Remapping a Digit.java
new file mode 100644
index 0000000000..9ad5fdd477
--- /dev/null
+++ b/Java/Maximum Difference By Remapping a Digit.java
@@ -0,0 +1,16 @@
+class Solution {
+
+ public int minMaxDifference(int num) {
+ String s = Integer.toString(num);
+ String t = s;
+ int pos = 0;
+ while (pos < s.length() && s.charAt(pos) == '9') {
+ pos++;
+ }
+ if (pos < s.length()) {
+ s = s.replace(s.charAt(pos), '9');
+ }
+ t = t.replace(t.charAt(0), '0');
+ return Integer.parseInt(s) - Integer.parseInt(t);
+ }
+}
From 502be1628ac77b70bc36cf57c06e4601648d7d07 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Sun, 15 Jun 2025 09:55:30 +0530
Subject: [PATCH 55/58] Create Max Difference You Can Get From Changing An
Integer.cpp
---
...e You Can Get From Changing An Integer.cpp | 34 +++++++++++++++++++
1 file changed, 34 insertions(+)
create mode 100644 C++/Max Difference You Can Get From Changing An Integer.cpp
diff --git a/C++/Max Difference You Can Get From Changing An Integer.cpp b/C++/Max Difference You Can Get From Changing An Integer.cpp
new file mode 100644
index 0000000000..9b7a51773e
--- /dev/null
+++ b/C++/Max Difference You Can Get From Changing An Integer.cpp
@@ -0,0 +1,34 @@
+class Solution {
+public:
+ int maxDiff(int num) {
+ string str1 = to_string(num);
+ string str2 = str1;
+ int n = str1.size();
+ // Get max number
+ int i;
+ for(i = 0; i < n; i++) {
+ if(str1[i] != '9') break;
+ }
+ char ele1 = str1[i];
+ for(int k = 0; k < n; k++) {
+ if(str1[k] == ele1) str1[k] = '9';
+ }
+ // Get min number
+ char ele2 = str2[0];
+ char replace = '1';
+ if(ele2 == '1') {
+ for(int k = 1; k < n; k++) {
+ if(str2[k] != '0' && str2[k] != '1') {
+ ele2 = str2[k];
+ replace = '0';
+ break;
+ }
+ }
+ }
+ for(int k = 0; k < n; k++) {
+ if(str2[k] == ele2) str2[k] = replace;
+ }
+
+ return stoi(str1) - stoi(str2);
+ }
+};
From dd7ecf498af446e4bf51d23dec82823c1cf33706 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Tue, 17 Jun 2025 08:48:09 +0530
Subject: [PATCH 56/58] Create Count the number of Arrays with K Matching
Adjacent Elements.java
---
...ays with K Matching Adjacent Elements.java | 55 +++++++++++++++++++
1 file changed, 55 insertions(+)
create mode 100644 Java/Count the number of Arrays with K Matching Adjacent Elements.java
diff --git a/Java/Count the number of Arrays with K Matching Adjacent Elements.java b/Java/Count the number of Arrays with K Matching Adjacent Elements.java
new file mode 100644
index 0000000000..02213c13bb
--- /dev/null
+++ b/Java/Count the number of Arrays with K Matching Adjacent Elements.java
@@ -0,0 +1,55 @@
+class Solution {
+ // Modulo for all calculations
+ int mod = 1_000_000_007;
+
+ // Arrays to store factorials and modular inverses
+ static long[] revs = new long[100001]; // Not used in this version
+ static int[] f = new int[100001]; // Factorials
+
+ // Main function to count good arrays
+ public int countGoodArrays(int n, int m, int k) {
+ // Base case: factorial[0] = 1
+ if (f[0] == 0)
+ f[0] = 1;
+
+ // Formula:
+ // m --> choose value for the first element
+ // pow(m-1, n-1-k) --> number of ways to keep same value (for n-1-k positions)
+ // C(n-1, n-1-k) --> choose positions to remain the same among (n-1)
+ long res = m * pow(m - 1, n - 1 - k) % mod * C(n - 1, n - 1 - k) % mod;
+
+ return (int) res;
+ }
+
+ // Fast exponentiation: computes (a^b) % mod
+ public long pow(int a, int b) {
+ long res = 1;
+ long base = a;
+ while (b > 0) {
+ if ((b & 1) == 1)
+ res = res * base % mod;
+ base = base * base % mod;
+ b /= 2;
+ }
+ return res;
+ }
+
+ // Computes nCr % mod using factorial and modular inverse
+ public long C(int a, int b) {
+ return (long) getF(a) * rev(getF(a - b)) % mod * rev(getF(b)) % mod;
+ }
+
+ // Computes factorial with memoization: f[a] = a!
+ public long getF(int a) {
+ if (f[a] != 0)
+ return f[a];
+ return f[a] = (int) (getF(a - 1) * a % mod);
+ }
+
+ // Modular inverse using Fermat's Little Theorem: a^(-1) ≡ a^(mod - 2)
+ public long rev(long a) {
+ if (a == 1)
+ return a;
+ return mod - mod / a * rev(mod % a) % mod;
+ }
+}
From 8f481bfbd319f5b832469bf51b813dcc1d640f33 Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Wed, 18 Jun 2025 09:08:32 +0530
Subject: [PATCH 57/58] Create Divide Array Into Arrays With Max Difference.cpp
---
...Divide Array Into Arrays With Max Difference.cpp | 13 +++++++++++++
1 file changed, 13 insertions(+)
create mode 100644 C++/Divide Array Into Arrays With Max Difference.cpp
diff --git a/C++/Divide Array Into Arrays With Max Difference.cpp b/C++/Divide Array Into Arrays With Max Difference.cpp
new file mode 100644
index 0000000000..ab357638da
--- /dev/null
+++ b/C++/Divide Array Into Arrays With Max Difference.cpp
@@ -0,0 +1,13 @@
+class Solution {
+public:
+ vector> divideArray(vector& nums, int k) {
+ vector> ans;
+ int n=nums.size();
+ sort(nums.begin(),nums.end());
+ for(int i=0;ik) return {};
+ ans.push_back({nums[i],nums[i+1],nums[i+2]});
+ }
+ return ans;
+ }
+};
From c5b75b78f21e4fde86511649f198065b71113a5f Mon Sep 17 00:00:00 2001
From: Dipanita Mondal <132455672+Dipanita45@users.noreply.github.com>
Date: Thu, 19 Jun 2025 09:10:26 +0530
Subject: [PATCH 58/58] Create Partition Array Such That Maximum Difference is
K.java
---
...n Array Such That Maximum Difference is K.java | 15 +++++++++++++++
1 file changed, 15 insertions(+)
create mode 100644 Java/Partition Array Such That Maximum Difference is K.java
diff --git a/Java/Partition Array Such That Maximum Difference is K.java b/Java/Partition Array Such That Maximum Difference is K.java
new file mode 100644
index 0000000000..cad0e6fa30
--- /dev/null
+++ b/Java/Partition Array Such That Maximum Difference is K.java
@@ -0,0 +1,15 @@
+class Solution {
+
+ public int partitionArray(int[] nums, int k) {
+ Arrays.sort(nums);
+ int ans = 1;
+ int rec = nums[0];
+ for (int i = 0; i < nums.length; i++) {
+ if (nums[i] - rec > k) {
+ ans++;
+ rec = nums[i];
+ }
+ }
+ return ans;
+ }
+}