Skip to content

Instantly share code, notes, and snippets.

View HarshKumarChoudary's full-sized avatar
🎯
Focusing

Harsh kumar choudhary HarshKumarChoudary

🎯
Focusing
  • Chhattisgarh, India
  • 14:29 (UTC +05:30)
View GitHub Profile
@HarshKumarChoudary
HarshKumarChoudary / Water the Plants
Created July 21, 2022 07:55
Best Approach Code Although there is one approach with O(N) also
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]);
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)
{
@HarshKumarChoudary
HarshKumarChoudary / See the logic of recursive inclusion and exclusion
Created June 13, 2022 06:54
A number k is called a square number if for some value of d > 1, k % (d*d) = 0. Given a number N, find the total number of positive square numbers less than or equal to N.
/*
// 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
@HarshKumarChoudary
HarshKumarChoudary / To Count pairs using rank in DSU
Created June 12, 2022 04:29
The director of your college is planning to send 2 people to the ICPC regionals. He wants them to be from different branches. You will be given a list of pairs of student ids. Each pair is made of students from the same branch. Determine how many pairs of students from different branches they can choose from. Example 1: Input: N=5 P=3 pairs[]={{…
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;
@HarshKumarChoudary
HarshKumarChoudary / Geek collects the balls
Created June 9, 2022 10:11
There are two parallel roads, each containing N and M buckets, respectively. Each bucket may contain some balls. The balls in first road are given in an array a and balls in the second road in an array b. The buckets on both roads are kept in such a way that they are sorted according to the number of balls in them. Geek starts from the end of th…
//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){
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--;
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;
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){
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];