Skip to content

Create Count Hidden Sequence.java #203

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 7 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);

}

};

11 changes: 11 additions & 0 deletions Java/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
<filteredResources>
<filter>
<id>1744612223460</id>
<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>
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;
}
}


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;
}
}
30 changes: 30 additions & 0 deletions Java/Maximum Value of an Ordered Triplets.java
Original file line number Diff line number Diff line change
@@ -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;
}
}