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

Updated the Coding section #439

Open
wants to merge 14 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
49 changes: 49 additions & 0 deletions Coding/C++/BIT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>
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;
}
99 changes: 99 additions & 0 deletions Coding/C++/Connnect_components_using_DFS.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include <bits/stdc++.h>
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<n;i++){
for(int j=0;j<m;j++){
cin>>adj[i][j];
}
}

memset(visited,0,sizeof visited);

vector<pair<int,pair<int,int>>> ans;
int cnt =0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(adj[i][j]=='.' and visited[i][j]==0){
int x=0;
dfs(i,j,x);
ans[cnt].first=x;
ans[cnt].second.first = i;
ans[cnt].second.second = j;
cnt++;
}
}
}


sort(ans.begin(),ans.end());
int res =0;
for(int i=0;i<ans.size()-k;i++){
res+=ans[i].first;
}
cout<<res<<"\n";



}

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);

#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif

long long t=1;
// cin>>t;
while(t--){
solve();
}
return 0;

}
83 changes: 83 additions & 0 deletions Coding/C++/Cycle_detection_using_D.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include <bits/stdc++.h>
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 <pair<ll,ll>> 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<ll>& parent){
if(parent[i]==-1){
return i;
}
return parent[i]=leader(parent[i],parent);
}

void union_set(ll x,ll y,vector<ll>& parent){
ll s1 = leader(x,parent);
ll s2 = leader(y,parent);
if(s1!=s2){
parent[s2]=s1;
}
}

bool cycle_detection(){
vector<ll> parent(V);
for(int i=0;i<V;i++){
parent[i]=-1;
}
for(auto i: l){
ll x = leader(i.first,parent);
ll y = leader(i.second,parent);
if(x!=y){
union_set(i.first,i.second,parent);
}else{
return true;
}
}
parent.clear();
return false;
}

};



int main(){

int t; cin>>t;
while(t--){
ll n;cin>>n;
graph p(n);ll flag=1;
vector<int> cnt(n);
for(int i=0;i<n;i++ ){
int a,b;cin>>a>>b;
a--; b--;
p.merge(a,b);
cnt[a]++; cnt[b]++;
if(cnt[a]>=3|| cnt[b]>=3){
flag=0;

}
}
}
}

62 changes: 62 additions & 0 deletions Coding/C++/Inversion_count_using_BIT.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include<bits/stdc++.h>
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<int> s;
for(int i=1;i<=n;i++){
cin>>a[i];
s.insert(a[i]);
}

map<int,int> m;
int count =1;
for(auto i: s){
m[i]=count;
count++;
}

vector<int> 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<<res<<"\n";

return 0;
}
97 changes: 97 additions & 0 deletions Coding/C++/Lazy_propagation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include<bits/stdc++.h>
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<l){
return;
}
//complete overlapp case
if(l<=ss and r>=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(qe<ss or qs>se){
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;
}
Loading