Skip to content
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

Sub array sum #966

Open
wants to merge 7 commits into
base: main
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
50 changes: 50 additions & 0 deletions Dynamic Programming/Medium/Sliding Window/longestSubstring.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include<bits/stdc++.h>
using namespace std;

class Solution {
public:
int longestSubstring(string s, int k) {
int countMap[26];
int maxUnique = getMaxUniqueLetters(s);
int result = 0;
for (int currUnique = 1; currUnique <= maxUnique; currUnique++) {
// reset countMap
memset(countMap, 0, sizeof(countMap));
int windowStart = 0, windowEnd = 0, idx = 0, unique = 0, countAtLeastK = 0;
while (windowEnd < s.size()) {
// expand the sliding window
if (unique <= currUnique) {
idx = s[windowEnd] - 'a';
if (countMap[idx] == 0) unique++;
countMap[idx]++;
if (countMap[idx] == k) countAtLeastK++;
windowEnd++;
}
// shrink the sliding window
else {
idx = s[windowStart] - 'a';
if (countMap[idx] == k) countAtLeastK--;
countMap[idx]--;
if (countMap[idx] == 0) unique--;
windowStart++;
}
if (unique == currUnique && unique == countAtLeastK)
result = max(windowEnd - windowStart, result);
}
}

return result;
}

int getMaxUniqueLetters(string s) {
bool map[26] = {0};
int maxUnique = 0;
for (int i = 0; i < s.length(); i++) {
if (!map[s[i] - 'a']) {
maxUnique++;
map[s[i] - 'a'] = true;
}
}
return maxUnique;
}
};
20 changes: 20 additions & 0 deletions Leetcode Solutions/max_subarray_sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include<bits/stdc++.h>
using namespace std;

// Maximum subarray sum problem of leetcode

class Solution {
public:
int maxSubArray(vector<int>& nums) {

int max = INT_MIN;
int sum = 0;
for(int i=0 ; i<nums.size() ; i++){
sum+=nums[i];
if(sum > max) max = sum;
if(sum < 0 ) sum = 0;
}
return max;

}
};
109 changes: 109 additions & 0 deletions Number Theory/divisorCntSieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// C++ program to count distinct divisors
// of a given number n using sieve
#include <bits/stdc++.h>
using namespace std;

void SieveOfEratosthenes(int n, bool prime[],
bool primesquare[], int a[])
{
for (int i = 2; i <= n; i++)
prime[i] = true;

for (int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false;

// 1 is not a prime number
prime[1] = false;

for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p starting from p * p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}

int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// Storing primes in an array
a[j] = p;

// Update value in primesquare[p*p],
// if p is prime.
primesquare[p * p] = true;
j++;
}
}
}

// Function to count divisors
int countDivisors(int n)
{
if (n == 1)
return 1;

bool prime[n + 1], primesquare[n * n + 1];

int a[n]; // for storing primes upto n

SieveOfEratosthenes(n, prime, primesquare, a);

// ans will contain total number of distinct
// divisors
int ans = 1;

// Loop for counting factors of n
for (int i = 0;; i++) {
// a[i] is not less than cube root n
if (a[i] * a[i] * a[i] > n)
break;

// Calculating power of a[i] in n.
int cnt = 1; // cnt is power of prime a[i] in n.
while (n % a[i] == 0) // if a[i] is a factor of n
{
n = n / a[i];
cnt = cnt + 1; // incrementing power
}

ans = ans * cnt;
}

// if a[i] is greater than cube root of n

// First case
if (prime[n])
ans = ans * 2;

// Second case
else if (primesquare[n])
ans = ans * 3;

// Third case
else if (n != 1)
ans = ans * 4;

return ans; // Total divisors
}

// Driver Program
int main()
{
int n;
cout << "Total distinct divisors of 100 are : ";
cin >> n;



for(int i=1 ; i<=n ; i++){
cout<<"Total number of Divisors of "<<i<<" are : ";
cout<<countDivisors(i);
cout<<endl;
}


return 0;
}
109 changes: 109 additions & 0 deletions Number Theory/numberOfDivisorSieve.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// C++ program to count distinct divisors
// of a given number n using sieve
#include <bits/stdc++.h>
using namespace std;

void SieveOfEratosthenes(int n, bool prime[],
bool primesquare[], int a[])
{
for (int i = 2; i <= n; i++)
prime[i] = true;

for (int i = 0; i <= (n * n + 1); i++)
primesquare[i] = false;

// 1 is not a prime number
prime[1] = false;

for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then
// it is a prime
if (prime[p] == true) {
// Update all multiples of p starting from p * p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}

int j = 0;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// Storing primes in an array
a[j] = p;

// Update value in primesquare[p*p],
// if p is prime.
primesquare[p * p] = true;
j++;
}
}
}

// Function to count divisors
int countDivisors(int n)
{
if (n == 1)
return 1;

bool prime[n + 1], primesquare[n * n + 1];

int a[n]; // for storing primes upto n

SieveOfEratosthenes(n, prime, primesquare, a);

// ans will contain total number of distinct
// divisors
int ans = 1;

// Loop for counting factors of n
for (int i = 0;; i++) {
// a[i] is not less than cube root n
if (a[i] * a[i] * a[i] > n)
break;

// Calculating power of a[i] in n.
int cnt = 1; // cnt is power of prime a[i] in n.
while (n % a[i] == 0) // if a[i] is a factor of n
{
n = n / a[i];
cnt = cnt + 1; // incrementing power
}

ans = ans * cnt;
}

// if a[i] is greater than cube root of n

// First case
if (prime[n])
ans = ans * 2;

// Second case
else if (primesquare[n])
ans = ans * 3;

// Third case
else if (n != 1)
ans = ans * 4;

return ans; // Total divisors
}

// Driver Program
int main()
{
int n;
cout << "Total distinct divisors of 100 are : ";
cin >> n;



for(int i=1 ; i<=n ; i++){
cout<<"Total number of Divisors of "<<i<<" are : ";
cout<<countDivisors(i);
cout<<endl;
}


return 0;
}
24 changes: 24 additions & 0 deletions Stack/minStack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;

// Min Stack Implementation that supports getMin() in O(1) time and O(1) extra space

class MinStack {
public:
vector< pair<int,int> > s;

MinStack() { }

void push(int val) {
if(s.empty())
s.push_back({val,val});
else
s.push_back({val,min(s.back().second,val)});
}

void pop() { s.pop_back(); }

int top() { return s.back().first; }

int getMin() { return s.back().second; }
};
24 changes: 24 additions & 0 deletions Stack/min__Stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;

// Min Stack Implementation that supports getMin() in O(1) time and O(1) extra space

class MinStack {
public:
vector< pair<int,int> > s;

MinStack() { }

void push(int val) {
if(s.empty())
s.push_back({val,val});
else
s.push_back({val,min(s.back().second,val)});
}

void pop() { s.pop_back(); }

int top() { return s.back().first; }

int getMin() { return s.back().second; }
};
24 changes: 24 additions & 0 deletions Stack/min_stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
using namespace std;

// Min Stack Implementation that supports getMin() in O(1) time and O(1) extra space

class MinStack {
public:
vector< pair<int,int> > s;

MinStack() { }

void push(int val) {
if(s.empty())
s.push_back({val,val});
else
s.push_back({val,min(s.back().second,val)});
}

void pop() { s.pop_back(); }

int top() { return s.back().first; }

int getMin() { return s.back().second; }
};