This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution{ | |
public: | |
int min_sprinklers(int gallery[], int n) | |
{ | |
int ans = 0; | |
vector<pair<int,int>>v; | |
for(int i = 0; i < n; ++ i){ | |
if(gallery[i] == -1)continue; | |
int left = max(0,i-gallery[i]); | |
int right = min(n-1,i+gallery[i]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int findDeterminant(vector<vector<int>>&Matrix){ | |
// Variable to store the determinant value | |
int det = 0; | |
if (Matrix.size() == 1) | |
{ | |
return Matrix[0][0]; | |
} | |
else if (Matrix.size() == 2) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// Difficulty : Hard Prerequisite: Inclusion-Exclusion Principles, Sieve of Eratosthenes Time complexity: O(sqrt(n)*log(n)) If any integer has a perfect square as factor, then it is guaranteed that it has a square of prime as a factor too. So we need to count the integers less than or equal to N which have square of primes as a factor. Let us look at the number of integers less than N which has 2*2=4 as factor. Every 4th number has 4 as factor(4,8,12,16..) so there are total [N/4] numbers who has 4 as factor, similarly there are [N/9] numbers who has 9 as factor. (Here [N] is greatest integer of N). Now to calculate the number of integer who has both 4,9 as factor, we cant simply add [N/4] and [N/9] as we are counting numbers with 36 as factor two times. To avoid this we use Inclusion-Exclusion Principle. To avoid the double counting of number with 36 as factor ,we subtract [N/36] from our answer. So number of integers with both 4,9 as factor=[N/4]+[N/9]-[N/36] Similarly, the number of integers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See description for link |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int>par; | |
vector<int>rank; | |
int find(int a){ | |
return par[a] == a?a:par[a] = find(par[a]); | |
} | |
void unin(int a,int b) | |
{ | |
if(a == b)return; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Geek collects the balls :- | |
// https://practice.geeksforgeeks.org/problems/geek-collects-the-balls5515/1/?page=1&status[]=unsolved&curated[]=7&sortBy=submissions# | |
class Solution{ | |
public: | |
int maxBalls(int n, int m, vector<int> a, vector<int> b){ | |
int i=0,j=0; | |
int s1=0,s2=0; | |
sort(a.begin(),a.end()); | |
sort(b.begin(),b.end()); | |
while(i<n || j<m){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
int permutation(int m, int n) { | |
// m number with len n | |
// m * (m - 1) * (m - 2) .. * (m - n + 1) | |
int res = 1; | |
while (n) { | |
res *= m; | |
m--; | |
n--; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int Solution::jump(vector<int> &a) { | |
int jmp = 0; | |
int cur = 0; | |
int next = 0; | |
for(int i = 0; i <= cur; ++ i){ | |
if(i == a.size()-1)return jmp; | |
next = max(next,a[i]+i); | |
if(i == cur){ | |
++jmp; | |
cur = next; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution | |
{ | |
public: | |
vector<int> findOrder(int n, int m, vector<vector<int>> pre) | |
{ | |
vector<int>ans; | |
vector<int>indeg(n,0); | |
vector<int>adj[n]; | |
queue<int>q; | |
for(auto a:pre){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
vector<int>ans; | |
vector<int>cnt; | |
vector<vector<int>>adj; | |
void dfs(int i,int cur){ | |
for(auto a:adj[i]){ | |
if(a!=cur){ | |
dfs(a,i); | |
cnt[i]+=cnt[a]; |
NewerOlder