From b7f57c5c8a22990790a91fa86ba7024f0c1f47ba Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:09:07 +0530 Subject: [PATCH 1/7] Added Connnect_components_using_DFS.cpp A code to count the connected components in a graph --- Coding/C++/Connnect_components_using_DFS.cpp | 99 ++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 Coding/C++/Connnect_components_using_DFS.cpp diff --git a/Coding/C++/Connnect_components_using_DFS.cpp b/Coding/C++/Connnect_components_using_DFS.cpp new file mode 100644 index 00000000..cd9d5888 --- /dev/null +++ b/Coding/C++/Connnect_components_using_DFS.cpp @@ -0,0 +1,99 @@ +#include +using namespace std; + +int n,m,k; +const int N = 100; +const int M = 100; +int visited[N][M]; +char adj[N][M]; + + + +void dfs(int i,int j,int &ans){ + + if((i>n-1 or j>m-1 or i<0 or j<0) ){ + return; + } + + if(adj[i][j]=='*'){ + return; + } + + visited[i][j]=1; + ans++; + + + for(int dx=-1;dx<=1;dx++){ + for(int dy=-1;dy<=1;dy++){ + if((abs(dx)+abs(dy))!=2){ + if(visited[i+dx][j+dy]==0){ + dfs(i+dx,j+dy,ans); + } + } + } + } + + if((i==n-1 or j==m-1 or i==0 or j==0) ){ + if(adj[i][j]=='.'){ + ans=0; + } + } + +} + +void solve(){ + cin>>n>>m>>k; + + for(int i=0;i>adj[i][j]; + } + } + + memset(visited,0,sizeof visited); + + vector>> ans; + int cnt =0; + for(int i=0;i>t; + while(t--){ + solve(); + } + return 0; + +} From 0bbfbb60c1b6b3c060d464be0acb848be87e006e Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:14:29 +0530 Subject: [PATCH 2/7] Added Cycle_detection_using_DSU.cpp Cycle_detection_using_DSU --- Coding/C++/Cycle_detection_using_D.cpp | 83 ++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 Coding/C++/Cycle_detection_using_D.cpp diff --git a/Coding/C++/Cycle_detection_using_D.cpp b/Coding/C++/Cycle_detection_using_D.cpp new file mode 100644 index 00000000..0eed7c7a --- /dev/null +++ b/Coding/C++/Cycle_detection_using_D.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; + +using ll = long long ; + +// DSU -> cycle detection in unidirected graphs (krukshal algorithm) +// implementing a data structure that will operate over disjoint sets +// main focus will be on union and find functions +// intial complexity = O(n) ; +// final after some optimisations =O(1); + +// path compression optimisation -->find function +// union by rank optimisation ---->union function + +class graph{ + ll V; + list > l; + public : + graph(ll V){ + this->V=V; + } + + void merge(ll u,ll v){ + l.push_back(make_pair(u,v)); + } + + ll leader(ll i,vector& parent){ + if(parent[i]==-1){ + return i; + } + return parent[i]=leader(parent[i],parent); + } + + void union_set(ll x,ll y,vector& parent){ + ll s1 = leader(x,parent); + ll s2 = leader(y,parent); + if(s1!=s2){ + parent[s2]=s1; + } + } + + bool cycle_detection(){ + vector parent(V); + for(int i=0;i>t; + while(t--){ + ll n;cin>>n; + graph p(n);ll flag=1; + vector cnt(n); + for(int i=0;i>a>>b; + a--; b--; + p.merge(a,b); + cnt[a]++; cnt[b]++; + if(cnt[a]>=3|| cnt[b]>=3){ + flag=0; + + } + } + } +} + From 5857377c4642f7e844ab4545f633a3fc0027cb83 Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:18:43 +0530 Subject: [PATCH 3/7] Added Union_Find.cpp Added the standard CP template for Union Find Algorithm --- Coding/C++/Union_Find.cpp | 43 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Coding/C++/Union_Find.cpp diff --git a/Coding/C++/Union_Find.cpp b/Coding/C++/Union_Find.cpp new file mode 100644 index 00000000..b55340e4 --- /dev/null +++ b/Coding/C++/Union_Find.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + + +struct DSU{ + vector p, sum; + DSU(long long n) : p(n),sum(n,1) { + iota(p.begin(), p.end(), 0); + } + long long leader(long long x){ + return p[x] == x?p[x]:p[x]=leader(p[x]); + } + bool same(long long u, long long v){ + return leader(u) == leader(v); + } + void merge(long long u, long long v) { + u = leader(u), v = leader(v); + if(sum[u]>n>>m; + vector a(n); + DSU f(n); + for(int i=0;i>a[i]; + f.sum[i]=a[i]; + } + for(int i=0;i>u>>v; + u--;v--; + f.merge(u,v); + } + + + return 0; +} From a7835ea5c9179fc5f976b053ac64c1c8ac45f67d Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:30:24 +0530 Subject: [PATCH 4/7] Added Segment_tree.cpp Added the standard template for CP of segment trees --- Coding/C++/Segment_tree.cpp | 84 +++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 Coding/C++/Segment_tree.cpp diff --git a/Coding/C++/Segment_tree.cpp b/Coding/C++/Segment_tree.cpp new file mode 100644 index 00000000..da57484c --- /dev/null +++ b/Coding/C++/Segment_tree.cpp @@ -0,0 +1,84 @@ +#include +using namespace std; + +void build_tree(vector &arr,long long s,long long e, vector &tree,long long index){ + //base case + if(s==e){ + tree[index]=arr[s]; + } + //recurssive case + long long mid = (s+e)/2 ; + build_tree(arr,s,mid,tree,2*index); + build_tree(arr,mid+1,e,tree,2*index+1); + + tree[index] = min(tree[2*index],tree[2*index+1]); + return ; +} + +long long query(vector &tree,long long ss,long long se,long long qs,long long qe,long long index){ + + //complete overlap + if(qe>=se and qs<=ss){ + return tree[index]; + } + //No overlap + if(qese){ + return INT_MAX; + } + //partial overlapp + long long mid =(ss+se)/2; + long long left = query(tree,ss,mid,qs,qe,2*index); + long long right = query(tree,mid+1,se,qs,qe,2*index+1); + + return min(left,right); + +} + + +void update(vector &tree,long long ss,long long se,long long i,long long increment,long long index){ + + //case where I is out of bound + if(ise){ + return ; + } + //leaf node + if(ss==se){ + tree[index]+=increment; + return ; + } + //otherwise + long long mid =(ss+se)/2; + update(tree,ss,mid,i,increment,2*index); + update(tree,mid+1,se,i,increment,2*index+1); + tree[index] = min(tree[2*index],tree[2*index+1]); + +} + +void update_range(vector &tree,long long ss,long long se,long long l,long long r,long long increment,long long index){ + + //out of bound + if(rse){ + return ; + } + //leaf node + if(ss==se){ + tree[index]+=increment; + return ; + } + //otherwise + long long mid =(ss+se)/2; + update_range(tree,ss,mid,l,r,increment,2*index); + update_range(tree,mid+1,se,l,r,increment,2*index+1); + tree[index] = min(tree[2*index],tree[2*index+1]); + +} + + + +int main(){ + + + return 0; + +} + From 24bdef8ff674154666f3d1b25a2edcaf4e9dc28d Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:33:58 +0530 Subject: [PATCH 5/7] Added Lazy_propagation.cpp It is an extension of the Segment tree where time complexity is reduced by propagating the updates in lazy fashion --- Coding/C++/Lazy_propagation.cpp | 97 +++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Coding/C++/Lazy_propagation.cpp diff --git a/Coding/C++/Lazy_propagation.cpp b/Coding/C++/Lazy_propagation.cpp new file mode 100644 index 00000000..1654e6f0 --- /dev/null +++ b/Coding/C++/Lazy_propagation.cpp @@ -0,0 +1,97 @@ +#include +using namespace std; + +int lazy[10000] ={0}; + +void update_range_lazy(int *tree,int ss,int se,int l,int r,int increment,int index){ + + // pahle backlog clear karo----------------------------------------------------------------------> + + //before going down resolve the values if it exists + if(lazy[index]!=0){ + tree[index]+=lazy[index]; + //non leaf node + if(ss!=se){ + lazy[2*index] =lazy[index]; + lazy[2*index+1] =lazy[index]; + } + //clearing the lazy value at current node + lazy[index]=0; + } + //base case--->no overlapp + if(ss>r or se=se){ + tree[index]+=increment; + if(ss!=se){ + lazy[2*index] +=increment; + lazy[2*index+1] +=increment; + } + return; + } + //Recurssive-->partial overlapping + int mid =(ss+se)/2; + update_range_lazy(tree,ss,mid,l,r,increment,index); + update_range_lazy(tree,mid+1,se,l,r,increment,index); + //update the tree + tree[index]= min(tree[2*index],tree[2*index+1]); + return ; + + +} + +int lazy_query(int *tree,int ss,int se,int qs,int qe,int index){ + //before going down resolve at the current node + if(lazy[index]!=0){ + tree[index]+=lazy[index]; + //non leaf node + if(ss!=se){ + lazy[2*index] =lazy[index]; + lazy[2*index+1] =lazy[index]; + } + //clearing the lazy value at current node + lazy[index]=0; + } + + //query logic + // no overlapping + if(qese){ + return INT_MAX; + } + //complete overlapping + if(qs<=ss and qe>=se){ + return tree[index]; + } + //partial overlapping + int mid = (ss+se)/2; + lazy_query(tree,ss,mid,qs,qe,2*index); + lazy_query(tree,mid+1,se,qs,qe,2*index+1); + tree[index] = min(tree[2*index],tree[2*index+1]); + + + return tree[index]; + +} + +void build_tree(int *arr,int s,int e,int *tree,int index){ + //base case + if(s==e){ + tree[index]=arr[s]; + } + //recurssive case + int mid = (s+e)/2 ; + build_tree(arr,s,mid,tree,2*index); + build_tree(arr,mid+1,e,tree,2*index+1); + + tree[index] = min(tree[2*index],tree[2*index+1]); + return ; +} + +int main (){ + + + + return 0; +} From 258a72791973eb84b9070e9a62f946b00a4fc936 Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:36:44 +0530 Subject: [PATCH 6/7] Added BIT.cpp An code optimised way of handling the range queries and updates but with some limitations along with usefulness in CP --- Coding/C++/BIT.cpp | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Coding/C++/BIT.cpp diff --git a/Coding/C++/BIT.cpp b/Coding/C++/BIT.cpp new file mode 100644 index 00000000..1540d119 --- /dev/null +++ b/Coding/C++/BIT.cpp @@ -0,0 +1,49 @@ +#include +using namespace std; + +const int N = 2e5+7; +int n; +int a[N]={0}; +int BIT[N]={0}; + + +// range sum query + +void update(int i,int ic){ + while(i<=n){ + BIT[i]+=ic; + i+=(i&(-i)); + } + return; +} + + +int query(int i){ + int re = 0; + while(i>0){ + re+=BIT[i]; + i-=(i&(-i)); + } + return re; +} + +int main(){ + + cin>>n; + memset(BIT,0,sizeof BIT); + for(int i=1;i<=n;i++){ + cin>>a[i]; + update(i,a[i]); + } + + int q; + cin>>q; + while(q--){ + int l,r; + cin>>l>>r; + cout<<(query(r)-query(l-1))<<"\n"; + } + + + return 0; +} From d8f016f05a6cf3cb76f80529fc44f3a7c65deade Mon Sep 17 00:00:00 2001 From: perfectdash <116922021+perfectdash@users.noreply.github.com> Date: Sat, 28 Oct 2023 11:38:37 +0530 Subject: [PATCH 7/7] Added Inversion_count_using_BIT.cpp A must question for interviews and coding test solved using BIT . --- Coding/C++/Inversion_count_using_BIT.cpp | 62 ++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Coding/C++/Inversion_count_using_BIT.cpp diff --git a/Coding/C++/Inversion_count_using_BIT.cpp b/Coding/C++/Inversion_count_using_BIT.cpp new file mode 100644 index 00000000..c53cf089 --- /dev/null +++ b/Coding/C++/Inversion_count_using_BIT.cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +const int N = 2e5+7; +int n; +int a[N]={0}; +int BIT[N]={0}; + +// range sum query + +void update(int i){ + while(i<=n){ + BIT[i]+=1; + i+=(i&(-i)); + } + return; +} + +int query(int i){ + int re =0; + while(i>0){ + re+=BIT[i]; + i-=(i&(-i)); + } + return re; +} + +int main(){ + + + // cordinate-compression + + cin>>n; + memset(BIT,0,sizeof BIT); + set s; + for(int i=1;i<=n;i++){ + cin>>a[i]; + s.insert(a[i]); + } + + map m; + int count =1; + for(auto i: s){ + m[i]=count; + count++; + } + + vector b(n+1); + for(int i=1;i<=n;i++){ + b[i]=m[a[i]]; + } + + int res =0; + for(int i=n;i>0;i--){ + res+=query(b[i]-1); + update(b[i]); + } + + cout<