Skip to content

Create Count of Subarrays in an Array #204

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 16 commits into
base: master
Choose a base branch
from
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);

}

};

13 changes: 13 additions & 0 deletions Java/.project
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@
<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;
}
}


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;

}
}
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;
}
}
28 changes: 28 additions & 0 deletions Java/Count of Subarrays in An Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution {
public int countCompleteSubarrays(int[] nums) {
int cnt = 0;
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
set.add(nums[i]);
}
HashMap<Integer, Integer> 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;
}
}
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;
}
}