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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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/10] 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