Skip to content

Create .java #208

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions C++/Combination Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<vector>
#include<stdio.h>

class Solution {

public:

vector<vector<int>> combinationSum(vector<int>& candidates, int target) {

vector<vector<int>> res;

vector<int> comb;

makeCombination(candidates, target, 0, comb, 0, res);

return res;

}

private:

void makeCombination(std::vector<int>& candidates, int target, int idx, vector<int>& comb, int total, vector<vector<int>>& 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);

}

};

17 changes: 17 additions & 0 deletions C++/Diameter of a Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
};
32 changes: 32 additions & 0 deletions C++/Lowest Common Ancestor of a Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
};
15 changes: 15 additions & 0 deletions Java/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<<<<<<< HEAD
<id>1745466012044</id>
=======
<id>1744612223460</id>
>>>>>>> ccb4566f91334ed4ca8447e7932db4fa2b1a10d1
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
11 changes: 11 additions & 0 deletions Java/Build Array from Permutation.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
17 changes: 17 additions & 0 deletions Java/Count Equal and Divisible in an Array.java
Original file line number Diff line number Diff line change
@@ -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<n-1;++i)
{
for(int j=i+1;j<n;++j)
{
if((i*j)%k==0 && nums[i]==nums[j])
{
++res;
}
}
}
return res;
}
}
19 changes: 19 additions & 0 deletions Java/Count Hidden Sequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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<n;i++){
prefix = prefix + differences[i];
min = Math.min(min,prefix);
max = Math.max(max,prefix);
if(max-min > diff) return 0;
}
return (diff) - (max -min) +1;
}
}


26 changes: 26 additions & 0 deletions Java/Count Largest Group.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public int countLargestGroup(int n) {
Map<Integer, Integer>hashMap=new HashMap<Integer,Integer>();
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.Entry<Integer,Integer>kvpair: hashMap.entrySet()){
if(kvpair.getValue()== maxValue)
{
++count;
}
}
return count;

}
}
Original file line number Diff line number Diff line change
@@ -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
}

}
19 changes: 19 additions & 0 deletions Java/Count Subarray Where max element appear more than k .java
Original file line number Diff line number Diff line change
@@ -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;
}
}
75 changes: 75 additions & 0 deletions Java/Count and Say.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
22 changes: 22 additions & 0 deletions Java/Count of Good Subarray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Solution {

public long countGood(int[] nums, int k) {
int n = nums.length;
int same = 0, right = -1;
HashMap<Integer, Integer> 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;
}
}
32 changes: 32 additions & 0 deletions Java/Count of Interested Subarrays.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution
{
public long countInterestingSubarrays(List<Integer> nums, int modulo, int k)
{
// Step 1 : Initialize result, prefix sum, and a map with base case
long result = 0;
int prefix = 0;
Map<Integer, Long> 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;
}
}
Loading