├── .github ├── FUNDING.yml └── ISSUE_TEMPLATE │ └── feature_request.md ├── 0-1 Knapsack.cpp ├── BFS.cpp ├── Binary Lifting.cpp ├── Binary Search Tree.cpp ├── Binary Tree.cpp ├── Bridges in Undirected Graph.cpp ├── Convex hull trick(Graham_scan).cpp ├── DFS.cpp ├── DFS └── Path Sum.cpp ├── Detect cycle in Directed graph.cpp ├── Dijkstra.cpp ├── Disjoint Set Union Algorithm.cpp ├── Disjoint Set Union.cpp ├── Euler Totient.cpp ├── Euler tour.cpp ├── Floyd Warshal.cpp ├── Hamming Distance.cpp ├── Height of N-ary tree.cpp ├── Kosaraju & Topological Sort.cpp ├── Kruskal.cpp ├── LICENSE ├── Lazy Segment Tree.cpp ├── LostGuyRadha.cpp ├── Lowest Common Ancestor.cpp ├── Maximum Spanning tree.cpp ├── Modular Multiplication and Exponentation.cpp ├── Negative Cycle in graph.cpp ├── Nuttela.cpp ├── PERL ├── 0_or_[aaa].pl ├── MCA..bw.pl ├── online_shop.html └── vowelcount.pl ├── Postorder, Preorder, Inorder.cpp ├── Prims.cpp ├── Priority Queue.cpp ├── Prufer to Tree.cpp ├── RapyutaTest.cpp ├── SPOJ ├── ANDROUND.cpp ├── ANT.cpp ├── DISQUERY.cpp ├── GSS1.cpp └── ORDERSET.cpp ├── SQRT_DECOMPOSITION.cpp ├── SegmentTree.cpp ├── Seive of erasthones.cpp ├── Size_of_subtree.cpp ├── Solution.java ├── Strongly_connected_component.cpp ├── Sum_of_subtree.cpp ├── TerribleMathematics.cpp ├── Testcase_generator(tree).cpp ├── Topological Sort.cpp ├── Treap.cpp ├── Tree to Prufer code.cpp ├── Trie.cpp ├── Unique Characters in String.cpp ├── Wavelet Tree.cpp ├── Z Algorithm.cpp ├── Zeller's congruence.py ├── hackerearth ├── Coloring trees.cpp ├── Help the Avengers.cpp ├── Maximum Spanning Tree.cpp ├── Micro and his Son.cpp ├── Monk and Square Root.cpp ├── Oz Number.cpp ├── Rest in peace - 21-1!.py ├── Sherlock and Date.cpp ├── Small Factorials.py ├── Tablets.py ├── Weighted String.cpp ├── Xenny and K-Equal-Triplets.cpp └── marks_time.cpp ├── matrix_mul.cpp └── readme.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: # Replace with a single Ko-fi username 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | otechie: # Replace with a single Otechie username 12 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 13 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /0-1 Knapsack.cpp: -------------------------------------------------------------------------------- 1 | 2 | int knapSack(int W,int wt[],int val[],int n){ 3 | int dp[n+1][W+1]; 4 | rep(i,0,n+1){ 5 | rep(j,0,W+1){ 6 | if(i==0||j==0) dp[i][j]=0; 7 | else if(wt[i-1] <= j)dp[i][j]=max(val[i-1] + dp[i-1][j-wt[i-1]], dp[i-1][j]); 8 | else dp[i][j] = dp[i-1][j]; 9 | } 10 | } 11 | return dp[n][W]; 12 | } 13 | int main() { 14 | IOS; 15 | int val[] = {7,5,2,1,3,4}; 16 | int wt[] = {10,2,4,5,1,3}; 17 | int W = 10; 18 | int n = sizeof(val)/sizeof(val[0]); 19 | printf("%d\n", knapSack(W, wt, val, n)); 20 | return 0; 21 | } 22 | 23 | 24 | -------------------------------------------------------------------------------- /BFS.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | BFS stands for Breadth first search. 3 | A searching technique used to traverse a tree. 4 | 5 | Consider the tree with 5 nodes and every (u -> v) edge added. 6 | 5 7 | 1 3 8 | 1 4 9 | 4 2 10 | 4 5 11 | 12 | Tree visual: 13 | 14 | 1 15 | / \ 16 | 3 4 17 | / \ 18 | 2 5 19 | 20 | BFS order. 21 | 1 3 4 2 5 22 | 23 | */ 24 | const int N = 1e6+6; 25 | int vis[N], g[N] 26 | void bfs() 27 | { 28 | queue q; 29 | q.push(1); 30 | while(!q.empty()) 31 | { 32 | int node = q.front(); 33 | cout << node << endl; 34 | vis[node]=1; 35 | q.pop(); 36 | for(auto it:g[node]) 37 | { 38 | if(!vis[it])q.push(it); 39 | } 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Binary Lifting.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Binary Lifting is an algorithm that is used in finding the Lowest Common ancestor of two nodes in a tree. 4 | Below is the clean implementation of the algorithm. 5 | 6 | For ref : https://www.geeksforgeeks.org/lca-in-a-tree-using-binary-lifting-technique/ 7 | 8 | */ 9 | int n,m,q,mx; 10 | const int MAX = 1e5+5; 11 | typedef pair pairs; 12 | const int LG = ceil(log2(MAX)); 13 | 14 | vector> up(MAX, vector(LG+1)); 15 | vector G[MAX]; 16 | 17 | void dfs(int u,int par){ 18 | up[u][0] = par; 19 | for(int i = 1; i <= LG; i++){ 20 | up[u][i]=up[up[u][i-1]][i-1]; 21 | } 22 | for(auto x : G[u]){ 23 | if(x!=par)dfs(x,u); 24 | } 25 | } 26 | 27 | -------------------------------------------------------------------------------- /Binary Search Tree.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela - Soham Chakrabarti */ 2 | /* 20 3 | / \ 4 | 8 22 5 | / \ 6 | 4 12 7 | / \ 8 | 10 14 9 | */ 10 | 11 | #include 12 | 13 | using namespace std; 14 | 15 | #define io ios_base::sync_with_stdio(false);cin.tie(NULL) 16 | #define all(v) v.begin(),v.end() 17 | #define pb push_back 18 | #define ins insert 19 | #define rep(i,j,k) for(ll i=j;i=k;--i) 21 | #define scan(a,n) rep(i,0,n)cin>>a[i] 22 | #define input freopen("input.txt","r",stdin) 23 | #define output freopen("output.txt","w",stdout) 24 | #define error freopen("error.txt","w",stderr) 25 | #define ff first 26 | #define ss second 27 | 28 | typedef long long int ll; 29 | typedef unsigned long long ull; 30 | typedef long double ld; 31 | 32 | const int N = 1e6+1; 33 | const int MAX = 1e6+1; 34 | const int inf = INT_MAX; 35 | const int mod = 1e9+7; 36 | 37 | ll powm(ll a,ll b) {ll res=1LL;while(b) {if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;} 38 | ll modmult(ll a,ll b) {ll r=0;a%=mod;while(b){if(b&1)r=(r+a)%mod;a=(a<<1)%mod;b>>=1;}return r;} 39 | ll modexpo(ll a,ll b) {ll r=1;a%=mod;while(b){if(b&1)r=(r*a)%mod;a=(a*a)%mod;b>>=1;}return r;} 40 | 41 | ll a[N],b[N]; 42 | int found,lca; 43 | 44 | class Node { 45 | int data; 46 | Node *left,*right; 47 | public: 48 | Node():data(0),left(NULL),right(NULL){}; 49 | Node (int val) { 50 | data=val; 51 | left=right=NULL; 52 | } 53 | /* Insertion in BST */ 54 | Node* insert(Node* rootNode,int val) { 55 | if(!rootNode) { 56 | return new Node(val); 57 | } 58 | if(rootNode->data > val) 59 | rootNode->left=insert(rootNode->left, val); 60 | else 61 | rootNode->right=insert(rootNode->right, val); 62 | return rootNode; 63 | } 64 | 65 | /* Inorder Print */ 66 | void inorder(Node* rootNode) { 67 | if(!rootNode)return; 68 | inorder(rootNode->left); 69 | printf("%d ",rootNode->data); 70 | inorder(rootNode->right); 71 | } 72 | 73 | /* Search Node */ 74 | void search(Node* rootNode,int val) { 75 | if(!rootNode) return; 76 | if(rootNode->data==val) { 77 | printf("%s%d\n","Found -> ",val); 78 | found=1; 79 | } 80 | search(rootNode->left,val); 81 | search(rootNode->right,val); 82 | } 83 | 84 | /* Lowest Common Ancestor */ 85 | 86 | void LCA(Node* rootNode,int u,int v) { 87 | if(!rootNode) return; 88 | // printf("%d\n",rootNode->data); 89 | if(rootNode->data>u&&rootNode->data>v) { 90 | LCA(rootNode->left,u,v); 91 | } 92 | else if(rootNode->datadataright,u,v); 94 | else { 95 | // printf("LCA:%d\n",rootNode->data); 96 | lca=rootNode->data; 97 | return; 98 | } 99 | 100 | } 101 | }; 102 | 103 | int32_t main() { 104 | io; 105 | 106 | Node n,*rootNode=NULL; 107 | rootNode=n.insert(rootNode,20); 108 | printf("%s\n","Root Created"); 109 | 110 | n.insert(rootNode, 8); 111 | n.insert(rootNode, 22); 112 | n.insert(rootNode, 4); 113 | n.insert(rootNode, 12); 114 | n.insert(rootNode, 10); 115 | n.insert(rootNode, 14); 116 | 117 | printf("%s","Inorder ->"); 118 | n.inorder(rootNode); 119 | printf("\n"); 120 | found=0; 121 | int key; 122 | key=2; 123 | n.search(rootNode,key); 124 | if(!found) 125 | printf("%s%d\n","Not Found -> ",key); 126 | key=5,found=0; 127 | n.search(rootNode,key); 128 | int u,v; 129 | printf("%s\n","U and V for LCA"); 130 | scanf("%d%d",&u,&v); 131 | n.LCA(rootNode,u,v); 132 | printf("%d\n",lca); 133 | return 0; 134 | } 135 | -------------------------------------------------------------------------------- /Binary Tree.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela - Soham Chakrabarti */ 2 | 3 | /* 4 | Using the Binary tree 5 | 6 | 10 7 | / \ 8 | 5 -3 9 | / \ \ 10 | 3 2 11 11 | / \ \ 12 | 3 -2 1 13 | 14 | Built a class TreeNode to maintain the Binary tree structure. 15 | 16 | Methods: 17 | 18 | 1. DFS(TreeNode*) 19 | 2. BFS(TreeNode*) 20 | */ 21 | 22 | 23 | #include 24 | 25 | using namespace std; 26 | 27 | #define io ios_base::sync_with_stdio(false);cin.tie(NULL) 28 | #define all(v) v.begin(),v.end() 29 | #define pb push_back 30 | #define ins insert 31 | #define rep(i,j,k) for(ll i=j;i=k;--i) 33 | #define scan(a,n) rep(i,0,n)cin>>a[i] 34 | #define input freopen("input.txt","r",stdin) 35 | #define output freopen("output.txt","w",stdout) 36 | #define error freopen("error.txt","w",stderr) 37 | #define ff first 38 | #define ss second 39 | 40 | typedef long long int ll; 41 | typedef unsigned long long ull; 42 | typedef long double ld; 43 | 44 | const int N = 2e5+1; 45 | const int MAX = 1e6+1; 46 | const int ALPHA = 26; 47 | const int inf = INT_MAX; 48 | const int mod = 1e9+7; 49 | 50 | ll powm(ll a,ll b) {ll res=1LL;while(b) {if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;} 51 | ll modmult(ll a,ll b) {ll r=0;a%=mod;while(b){if(b&1)r=(r+a)%mod;a=(a<<1)%mod;b>>=1;}return r;} 52 | ll modexpo(ll a,ll b) {ll r=1;a%=mod;while(b){if(b&1)r=(r*a)%mod;a=(a*a)%mod;b>>=1;}return r;} 53 | 54 | ll a[N], b[N]; 55 | 56 | class TreeNode { 57 | public: 58 | int val; 59 | TreeNode *left, *right; 60 | TreeNode(int x):val(x),left(NULL),right(NULL){} 61 | void dfs(TreeNode* root) { 62 | if(root) { 63 | cout << (root->val) << " "; 64 | dfs(root->left); 65 | dfs(root->right); 66 | } 67 | } 68 | 69 | void bfs(TreeNode* root) { 70 | if(!root) return; 71 | queue q; 72 | q.push(root); 73 | while(!q.empty()) { 74 | cout << (q.front()->val) << ' '; 75 | TreeNode* tmp = q.front(); 76 | q.pop(); 77 | if(tmp) { 78 | if(tmp->left)q.push(tmp->left); 79 | if(tmp->right)q.push(tmp->right); 80 | } 81 | } 82 | } 83 | 84 | void dfs(TreeNode* root, int currentSum, int sum, int &res) { 85 | if(root) { 86 | currentSum += root->val; 87 | if(currentSum == sum) ++res; 88 | dfs(root->left, currentSum, sum, res); 89 | dfs(root->right, currentSum, sum, res); 90 | } 91 | } 92 | 93 | void startFrom(TreeNode* root, int sum,int &res) { 94 | if(root) { 95 | dfs(root, 0, sum, res); 96 | startFrom(root->left, sum, res); 97 | startFrom(root->right, sum, res); 98 | } 99 | } 100 | 101 | int pathSum(TreeNode* root, int sum) { 102 | int res = 0; 103 | startFrom(root, sum, res); 104 | return res; 105 | } 106 | }; 107 | 108 | int main() { 109 | io; 110 | TreeNode* root = new TreeNode(10); 111 | root->left = new TreeNode(5); 112 | root->right = new TreeNode(-3); 113 | root->right->right = new TreeNode(11); 114 | root->left->left = new TreeNode(3); 115 | root->left->right = new TreeNode(2); 116 | root->left->right->right = new TreeNode(1); 117 | root->left->left->left = new TreeNode(3); 118 | root->left->left->right = new TreeNode(-2); 119 | 120 | cout << "DFS : " ; 121 | root->dfs(root); 122 | cout << endl; 123 | 124 | cout << "bfs : " ; 125 | root->bfs(root); 126 | cout << endl; 127 | 128 | /* Find the number of paths that sum to a given value. 129 | 130 | The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).*/ 131 | 132 | cout << "Total paths with sum = 8 : "<pathSum(root, 8) << endl; 133 | return 0; 134 | } 135 | -------------------------------------------------------------------------------- /Bridges in Undirected Graph.cpp: -------------------------------------------------------------------------------- 1 | /* take template from Nuttela.cpp */ 2 | 3 | vector g[MAX]; 4 | int in[MAX],low[MAX],vis[MAX]; 5 | int time=0; 6 | void dfs(int u,int par) { 7 | vis[u]=1; 8 | in[u]=low[u]=time++; 9 | for(auto &x:g[u]) { 10 | if(x==par)continue; 11 | if(vis[x])low[u]=min(in[x],low[u]); 12 | else { 13 | dfs(x,u); 14 | low[u]=min(low[u],low[x]); 15 | if(low[x]>in[u]) { 16 | ++bridge; 17 | // is_bridge(u,x); 18 | } 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Convex hull trick(Graham_scan).cpp: -------------------------------------------------------------------------------- 1 | 2 | struct point 3 | { 4 | int x, y; 5 | 6 | point () {} 7 | 8 | point(int x, int y) : x(x), y(y) {} //InitializerList usage 9 | /* ....Same as below cons 10 | 11 | point(int a, int b) { 12 | x=a; 13 | y=b; 14 | } 15 | */ 16 | }; 17 | 18 | bool comp(point &p1, point &p2) 19 | { 20 | if(p1.x!=p2.x) 21 | return p1.x0; 37 | } 38 | 39 | vector convex_hull(vector &v) 40 | { 41 | if(v.size()==1) 42 | return v; 43 | 44 | sort(v.begin(), v.end(), comp); 45 | 46 | point p1=v[0], p2=v.back(); 47 | //cout < up, down; 51 | up.push_back(p1); 52 | down.push_back(p1); 53 | 54 | for(int i=1;i=2 && !cw(up[up.size()-2], up[up.size()-1], v[i])) 59 | up.pop_back(); 60 | up.push_back(v[i]); 61 | } 62 | if(i==v.size()-1 || ccw(p1, v[i], p2)) 63 | { 64 | while(down.size()>=2 && !ccw(down[down.size()-2], down[down.size()-1], v[i])) 65 | down.pop_back(); 66 | down.push_back(v[i]); 67 | } 68 | } 69 | 70 | for(int i=down.size()-2;i>0;i--) 71 | up.push_back(down[i]); 72 | 73 | return up; 74 | } 75 | 76 | int n, m; 77 | vector poly1, poly2, hull1, hull2; 78 | vector v1, v2; 79 | 80 | int32_t main() 81 | { 82 | IOS; 83 | cin>>n; 84 | for(int i=1;i<=n;i++) 85 | { 86 | int x, y; 87 | cin>>x>>y; 88 | point p(x, y); 89 | poly1.push_back(p); 90 | } 91 | 92 | hull1=convex_hull(poly1); 93 | 94 | cout<<"--------------"< G[M]; 10 | vector vis(M); 11 | 12 | void dfs(int u) 13 | { 14 | vis[u] = true; 15 | cout << u << endl; 16 | for(int child : G[u]) 17 | { 18 | if(!vis[child]) 19 | dfs(child); 20 | } 21 | } 22 | 23 | /* DFS of a Undirected tree/graph */ 24 | void dfs(int u, int par) 25 | { 26 | cout << u << endl; 27 | for(int child : G[u]) 28 | { 29 | if(child != par) 30 | dfs(child); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /DFS/Path Sum.cpp: -------------------------------------------------------------------------------- 1 | /* Take template from Nuttela.cpp */ 2 | 3 | /* path with maximum sum in tree (positive values only) Use value array to store values */ 4 | 5 | vector g[MAX]; 6 | int dp[MAX]; 7 | 8 | void dfs(int u,int par,int &res) { 9 | dp[u]=u+dp[par]; 10 | for(int x:g[u]) { 11 | if(x==par) continue; 12 | dp[x]=x+dp[u]; 13 | dfs(x,u,res); 14 | } 15 | res=max(res,dp[u]); 16 | } 17 | 18 | int32_t main() { 19 | io; 20 | int n; 21 | cin>>n; 22 | rep(i,1,n) { 23 | int u,v; 24 | cin>>u>>v; 25 | g[u].pb(v); 26 | g[v].pb(u); 27 | } 28 | int res=-1; 29 | memset(dp,0,sizeof(dp)); 30 | dfs(1,0,res); 31 | cout< G[MAX],color; 10 | int arrival[MAX],departure[MAX],visited[MAX],path[MAX]; 11 | 12 | //0 white 13 | //1 gray 14 | //2 black 15 | bool findCycle(int u) 16 | { 17 | color[u] = 1; 18 | 19 | for(auto x : G[u]) 20 | { 21 | if(color[x] == 1)return true; 22 | if(color[x] == 0 && findCycle(x)) return true; 23 | }color[u] = 2; return false; 24 | 25 | } 26 | int32_t main() { 27 | IOS; 28 | n=4; 29 | G[1].push_back(2); 30 | G[1].push_back(3); 31 | G[3].push_back(2); 32 | G[3].push_back(4); 33 | 34 | int fl=0; 35 | color.resize(n,0); 36 | for (int x = 1; x <=n; ++x) 37 | { 38 | 39 | if(color[x] == 0)if(findCycle(x)==true)fl=1; else fl=2; 40 | } 41 | (fl==1)?cout<<"YES":cout<<"NO"; 42 | cout << '\n'; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Dijkstra.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | const int MOD = 1e9+7; 4 | const int MAX = 1e7; 5 | vector dis; 6 | vector processed(MAX, false); 7 | 8 | const int inf = INFINITY; 9 | void dijkstra(int source){ 10 | priority_queue> q; 11 | q.push({0,source}); 12 | dis.resize(MAX,inf); 13 | dis[source] = 0; 14 | 15 | while(!q.empty()){ 16 | int u = q.top().second; 17 | q.pop(); 18 | if(processed[u]) continue; 19 | processed[u] = true; 20 | for(auto y : G[u]){ 21 | int v = y.first, w = y.second; 22 | if(dis[u] + w < dis[v]){ 23 | dis[v] = dis[u] + w; 24 | q.push(-dis[v],v); 25 | } 26 | } 27 | } 28 | } 29 | int main() 30 | { 31 | dijkstra(source); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /Disjoint Set Union Algorithm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int get_superparent(int x,vectorarr,vectorpar){ 5 | if(par[x]==x)return x; 6 | else{ 7 | return par[x]=get_superparent(par[x],arr,par); 8 | } 9 | } 10 | void union_func(int a,int b,int &component,vectorarr,vector &par){ 11 | int x=get_superparent(a,arr,par); 12 | int y=get_superparent(b,arr,par); 13 | // cout<<"superParent of "<>n; 24 | vector arr(n); 25 | for(int i=0;i>arr[i]; 26 | component=n; 27 | vector par(n); 28 | for(int i=0;i>q; 30 | //mark superParent and parent of each node is self 31 | for(int i=0;i>a>>b; 33 | union_func(a,b,component,arr,par); 34 | } 35 | cout< p; 30 | int n; 31 | 32 | dsu(int _n) : n(_n) { 33 | p.resize(n); 34 | iota(p.begin(), p.end(), 0); 35 | } 36 | 37 | inline int get(int x) { 38 | return (x == p[x] ? x : (p[x] = get(p[x]))); 39 | } 40 | 41 | inline bool unite(int x, int y) { 42 | x = get(x); 43 | y = get(y); 44 | if (x != y) { 45 | p[x] = y; 46 | return true; 47 | } 48 | return false; 49 | } 50 | }; 51 | -------------------------------------------------------------------------------- /Euler Totient.cpp: -------------------------------------------------------------------------------- 1 | int phi(int n) { 2 | int result = n; 3 | for (int i = 2; i * i <= n; i++) { 4 | if(n % i == 0) { 5 | while(n % i == 0) 6 | n /= i; 7 | result -= result / i; 8 | } 9 | } 10 | if(n > 1) 11 | result -= result / n; 12 | return result; 13 | } 14 | -------------------------------------------------------------------------------- /Euler tour.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 1 3 | / \ 4 | 2 3 5 | / \ 6 | 4 5 7 | 8 | 9 | Euler Tour : 1 2 4 2 5 2 1 3 1 10 | 11 | Ref : https://www.geeksforgeeks.org/euler-tour-tree/ 12 | */ 13 | 14 | vector G[MAX], euler_tour; 15 | 16 | void dfs(int u,int parent){ 17 | euler_tour.pb(u); 18 | for(auto x:G[u]){ 19 | if(x==parent)continue; 20 | dfs(x,u); 21 | euler_tour.pb(u); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Floyd Warshal.cpp: -------------------------------------------------------------------------------- 1 | 2 | /* Ref : https://www.geeksforgeeks.org/floyd-warshall-algorithm-dp-16/ */ 3 | 4 | const int N = 1e6+6; 5 | 6 | int dist[N][N]; 7 | 8 | void floydWarshall(){ 9 | for (int k = 1; k <= n; k++) { 10 | for (int i = 1; i <= n; i++) { 11 | for (int j = 1; j <= n; j++) { 12 | distance[i][j] = min(distance[i][j], 13 | distance[i][k]+distance[k][j]); 14 | } 15 | } 16 | } 17 | } 18 | 19 | -------------------------------------------------------------------------------- /Hamming Distance.cpp: -------------------------------------------------------------------------------- 1 | /* Faced the problem in GOCC 21 SWE Google Grad */ 2 | 3 | /* You are given two arrays A and B (consider 1-based indexed). Both have N integers. 4 | You are given M list of pairs of indices, whose val can be swapped in array A only. 5 | 6 | For example (i, j) 1 <= i, j <= N , you can swap a[i] and a[j] 7 | 8 | The hamming distance between two arrays is the count of indexes where A[i] NOT equal to B[i]. 9 | 10 | You can swap the M pairs (as many times as you want, includung zero), Find the minimum Hamming Distance. 11 | 12 | 13 | Input : 14 | 15 | N, M 16 | Array A 17 | Array B 18 | . 19 | . 20 | .M pairs 21 | 22 | Output : 23 | 24 | Single Integer(H. D) 25 | I/O displayed at end. 26 | 27 | Code Below -> 28 | */ 29 | 30 | 31 | /* nuttela - Soham Chakrabarti */ 32 | 33 | #include 34 | 35 | using namespace std; 36 | 37 | #define io ios_base::sync_with_stdio(false);cin.tie(NULL) 38 | #define all(v) v.begin(),v.end() 39 | #define pb push_back 40 | #define ins insert 41 | #define rep(i,j,k) for(ll i=j;i=k;--i) 43 | #define scan(a,n) rep(i,0,n)cin>>a[i] 44 | #define input freopen("input.txt","r",stdin) 45 | #define output freopen("output.txt","w",stdout) 46 | #define error freopen("error.txt","w",stderr) 47 | #define ff first 48 | #define ss second 49 | 50 | typedef long long int ll; 51 | typedef unsigned long long ull; 52 | typedef long double ld; 53 | 54 | const int N = 1e6+1; 55 | const int MAX = 1e6+1; 56 | const int ALPHA = 26; 57 | const int inf = INT_MAX; 58 | const int mod = 1e9+7; 59 | 60 | ll powm(ll a,ll b) {ll res=1LL;while(b) {if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;} 61 | ll modmult(ll a,ll b) {ll r=0;a%=mod;while(b){if(b&1)r=(r+a)%mod;a=(a<<1)%mod;b>>=1;}return r;} 62 | ll modexpo(ll a,ll b) {ll r=1;a%=mod;while(b){if(b&1)r=(r*a)%mod;a=(a*a)%mod;b>>=1;}return r;} 63 | 64 | ll a[MAX], b[MAX]; 65 | vector g[MAX], vis1(MAX), vis2(MAX); 66 | multiset el; 67 | int ans; 68 | 69 | void dfs1(int u) { 70 | vis1[u]=1; 71 | el.insert(a[u]); 72 | for(auto &x:g[u]) { 73 | if(!vis1[x])dfs1(x); 74 | } 75 | } 76 | 77 | void dfs2(int u) { 78 | vis2[u]=1; 79 | if(el.count(b[u])) { 80 | el.erase(el.lower_bound(b[u])); 81 | } 82 | else 83 | ++ans; 84 | for(auto &x:g[u]) { 85 | if(!vis2[x])dfs2(x); 86 | } 87 | } 88 | 89 | int32_t main() { 90 | io; 91 | // input; output; 92 | int t; 93 | cin>>t; 94 | while(t--) { 95 | int n,m; 96 | ans=0; 97 | el.clear(); 98 | cin>>n>>m; 99 | scan(a,n); 100 | scan(b,n); 101 | rep(i,0,m) { 102 | int u,v; 103 | cin>>u>>v; 104 | --u,--v; 105 | if(u!=v) { 106 | g[u].pb(v); 107 | g[v].pb(u); 108 | } 109 | } 110 | 111 | rep(i,0,n) { 112 | if(!vis1[i]) { 113 | dfs1(i); 114 | dfs2(i); 115 | el.clear(); 116 | } 117 | } 118 | rep(i,0,MAX) { 119 | g[i].clear(); 120 | vis1[i]=vis2[i]=0; 121 | } 122 | cout< 3 | using namespace std; 4 | const int N=1e6+6; 5 | vector t[N]; 6 | int visited[N]; 7 | int level = -1; 8 | void dfs(int x,int y){ 9 | if(!visited[x])visited[x]=true; 10 | for(auto i:t[x]){ 11 | if(visited[i])continue; 12 | cout<> q; 19 | q.push({x,level}); 20 | while(!q.empty()){ 21 | int n=q.front().first; 22 | cout< 4 | using namespace std; 5 | 6 | #define ll long long 7 | #define ull unsigned long long 8 | #define ld long double 9 | #define pb push_back 10 | #define ppb pop_back 11 | #define pii pair 12 | #define pll pair 13 | #define vi vector 14 | #define vll vector 15 | #define vull vector 16 | #define vpii vector 17 | #define vpll vector 18 | #define mt make_tuple 19 | #define ff first 20 | #define ss second 21 | #define uset unordered_set 22 | #define umap unordered_map 23 | #define all(x) x.begin(), x.end() 24 | #define revall(x) x.rbegin(), x.rend() 25 | #define rep(i, j, k) for(ll i = j; i < k; ++i) 26 | #define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); 27 | #define TT int tt; cin>>tt; while(tt--) 28 | 29 | const ll MOD = (ll)(1e9+7); 30 | const int inf = (int)INFINITY; 31 | const ll INF = (ll)INFINITY; 32 | const int MAX = (int)(1e5+1); 33 | 34 | int n, m, ind = 0; 35 | ll maxval = 0; 36 | vi G[MAX], GR[MAX], T[MAX], GG[MAX], proc, cost(MAX), val(MAX), comp, arr; 37 | bool vis[MAX] = {false}; 38 | umap mp; 39 | 40 | void dfs1(int u) { 41 | vis[u] = true; 42 | for(auto v : GR[u]) 43 | if(!vis[v]) 44 | dfs1(v); 45 | proc.pb(u); 46 | } 47 | 48 | void dfs2(int u) { 49 | vis[u] = true; 50 | for(auto v : G[u]) 51 | if(!vis[v]) 52 | dfs2(v); 53 | comp.pb(u); 54 | } 55 | 56 | void kosaraju() { 57 | rep(i, 1, n+1) 58 | if(!vis[i]) 59 | dfs1(i); 60 | memset(vis, false, sizeof(vis)); 61 | for(int i = n-1; i >= 0; --i) 62 | if(!vis[proc[i]]) { 63 | ll sum = 0; 64 | dfs2(proc[i]); 65 | for(auto x : comp) sum += cost[x]; 66 | for(auto x : comp) { 67 | T[ind].pb(x); 68 | mp[x] = ind; 69 | val[x] = sum; 70 | } 71 | ++ind; 72 | comp.clear(); 73 | } 74 | } 75 | 76 | void dfs(int u) { 77 | vis[u] = true; 78 | for(auto v : GG[u]) 79 | if(!vis[v]) 80 | dfs(v); 81 | arr.pb(u); 82 | } 83 | 84 | void toposort() { 85 | memset(vis, false, sizeof(vis)); 86 | rep(i, 1, n+1) 87 | if(!vis[i]) 88 | dfs(i); 89 | } 90 | 91 | int main() { 92 | fastio; 93 | //TT { 94 | cin >> n >> m; 95 | rep(i, 1, n+1) cin >> cost[i]; 96 | rep(i, 0, m) { 97 | int u, v; 98 | cin >> u >> v; 99 | G[u].pb(v); 100 | GR[v].pb(u); 101 | } 102 | kosaraju(); 103 | rep(i, 0, ind) { 104 | for(auto u : T[i]) { 105 | for(auto v : G[u]) 106 | if(mp[v] != i) 107 | GG[i].pb(mp[v]); 108 | } 109 | } 110 | toposort(); 111 | rep(i, 0, arr.size()) { 112 | ll sum = 0; 113 | for(auto u : T[arr[i]]) { 114 | for(auto v : G[u]) 115 | if(mp[v] != mp[u]) 116 | sum = max(sum, val[v]); 117 | } 118 | for(auto u : T[arr[i]]) 119 | val[u] += sum; 120 | } 121 | rep(i, 1, n+1) cout << val[i] << " "; 122 | rep(i, 0, n+1) { 123 | G[i].clear(); 124 | GR[i].clear(); 125 | } 126 | proc.clear(); 127 | arr.clear(); 128 | memset(vis, false, sizeof(vis)); 129 | //} 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /Kruskal.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct edge{int src,des,weight;}; 6 | 7 | class UnionFind { 8 | int *parent, *ranks, _size; 9 | public: 10 | UnionFind(){ 11 | } 12 | UnionFind(int size){ 13 | parent = new int[size]; ranks = new int[size]; 14 | for(int element = 0 ; element < size ; element++){ 15 | parent[element] = element , ranks[element] = 0 ; 16 | } 17 | _size = size; 18 | } 19 | void resize(int size){ 20 | parent = new int[size]; ranks = new int[size]; 21 | for(int element = 0 ; element < size ; element++){ 22 | parent[element] = element , ranks[element] = 0 ; 23 | } 24 | _size = size; 25 | } 26 | int find(int element){ 27 | if(parent[element] == element){ 28 | return element; 29 | } 30 | else{ 31 | return parent[element] = find(parent[element]); // Path Compression algorithm 32 | } 33 | } 34 | bool connected(int x,int y){ 35 | if(find(x) == find(y)){ 36 | return true; 37 | } 38 | else{ 39 | return false; 40 | } 41 | } 42 | void merge(int x,int y){ 43 | x = find(x); 44 | y = find(y); 45 | if(x != y){ // Union by Rank algorithm 46 | if(ranks[x] > ranks[y]){ 47 | parent[y] = x; 48 | } 49 | else if(ranks[x] < ranks[y]){ 50 | parent[x] = y; 51 | } 52 | else{ 53 | parent[x] = y; ranks[y] ++ ; 54 | } 55 | _size--; 56 | } 57 | } 58 | void clear(){ 59 | delete [] parent; delete [] ranks; 60 | } 61 | int size(){ 62 | return _size; 63 | } 64 | }; 65 | bool comparator(const edge &a,const edge &b){ 66 | return a.weight < b.weight; 67 | } 68 | 69 | vector kruskalsAlgorithm(vectorgraph,int vertices){ 70 | UnionFind uf(vertices); 71 | vectorspanningTree; 72 | sort(graph.begin(),graph.end(),comparator); 73 | spanningTree.push_back(graph[0]); 74 | uf.merge(graph[0].src,graph[0].des); 75 | for(int i=1;igraph; 85 | int e,v; 86 | cin >> e >> v; 87 | graph.resize(e); 88 | for(int i=0;i> graph[i].src >> graph[i].des >> graph[i].weight; 90 | } 91 | vector spanningTree = kruskalsAlgorithm(graph,v); 92 | for(edge x : spanningTree){ 93 | cout << x.src << " " << x.des << " " << x.weight << endl; 94 | } 95 | return 0; 96 | } 97 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Soham Chakrabarti 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Lazy Segment Tree.cpp: -------------------------------------------------------------------------------- 1 | void build(int node,int s,int e){ 2 | if(s==e){ 3 | T[node] = arr[s]; 4 | return; 5 | } 6 | int m=(s+e)>>1; 7 | build(node<<1,s,m); 8 | build(node<<1|1,m+1,e); 9 | T[node]=(T[node<<1]+T[node<<1|1]); 10 | } 11 | int query_tree(int node, int a, int b, int i, int j) { 12 | 13 | if(a > b || a > j || b < i) return 0; // Out of range 14 | 15 | if(lazy[node] != 0) { // This node needs to be updated 16 | T[node] += lazy[node]; // Update it 17 | 18 | if(a != b) { 19 | lazy[node*2] += lazy[node]; // Mark child as lazy 20 | lazy[node*2+1] += lazy[node]; // Mark child as lazy 21 | } 22 | 23 | lazy[node] = 0; // Reset it 24 | } 25 | 26 | if(a >= i && b <= j) // Current segment is totally within range [i, j] 27 | return T[node]; 28 | 29 | int q1 = query_tree(node*2, a, (a+b)/2, i, j); // Query left child 30 | int q2 = query_tree(1+node*2, 1+(a+b)/2, b, i, j); // Query right child 31 | 32 | int res = (q1+ q2); // Return final result 33 | 34 | return res; 35 | } 36 | void upd(int node,int a,int b,int i, int j,int value){ 37 | if(lazy[node] != 0) { // This node needs to be updated 38 | T[node] += lazy[node]; // Update it 39 | 40 | if(a != b) { 41 | lazy[node*2] += lazy[node]; // Mark child as lazy 42 | lazy[node*2+1] += lazy[node]; // Mark child as lazy 43 | } 44 | 45 | lazy[node] = 0; // Reset it 46 | } 47 | 48 | if(a > b || a > j || b < i) // Current segment is not within range [i, j] 49 | return; 50 | 51 | if(a >= i && b <= j) { // Segment is fully within range 52 | T[node] += value; 53 | 54 | if(a != b) { // Not leaf node 55 | lazy[node*2] += value; 56 | lazy[node*2+1] += value; 57 | } 58 | 59 | return; 60 | } 61 | 62 | upd(node*2, a, (a+b)/2, i, j, value); // Updating left child 63 | upd(1+node*2, 1+(a+b)/2, b, i, j, value); // Updating right child 64 | 65 | T[node] = (T[node*2] + T[node*2+1]); // Updating root with max value 66 | } 67 | -------------------------------------------------------------------------------- /LostGuyRadha.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela - Soham Chakrabarti */ 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define IO ios_base::sync_with_stdio(false);cin.tie(NULL); 8 | #define endl '\n' 9 | 10 | typedef long long ll; 11 | typedef long double ld; 12 | typedef pair pii; 13 | 14 | const int N = 1e3; 15 | const int inf = INT_MAX; 16 | const int MOD = 1e9 + 7; 17 | 18 | int main() { 19 | ll tt; cin >> tt; 20 | while(tt--) { 21 | int n, q; 22 | cin >> n >> q; 23 | int f[n]; 24 | set a; 25 | for (int i = 0; i < n; ++i) 26 | { 27 | int x; 28 | cin >> x; 29 | a.insert(x); 30 | f[i] = *--a.end(); //last element of set 31 | } 32 | for (int i = 0; i < q; ++i) 33 | { 34 | int x; 35 | cin >> x; 36 | cout << f[x - 1] << endl; 37 | } 38 | } 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Lowest Common Ancestor.cpp: -------------------------------------------------------------------------------- 1 | /* Problem Link - https://www.codechef.com/problems/ENOC1 2 | Solution Link - https://www.codechef.com/viewsolution/27790936 3 | */ 4 | 5 | const int MAX = (int)1e5+1, LG = ceil(log2(MAX)); 6 | 7 | vector G[MAX]; 8 | vector lvl(MAX), val; 9 | vector> up(MAX, vector(LG+1)), dp(MAX, vector(LG+1)); 10 | 11 | void dfs(int u, int par, int h=0){ 12 | lvl[u] = h; 13 | up[u][0] = par; 14 | FR(i,1,LG+1){ 15 | up[u][i] = up[up[u][i-1]][i-1]; 16 | } 17 | for(auto x:G[u]){ 18 | if(x != par) dfs(x, u, h+1); 19 | } 20 | } 21 | 22 | int lca(int u, int v) { 23 | if(lvl[u] < lvl[v]) 24 | swap(u, v); 25 | for(int i = LG; i >= 0; --i) { 26 | if(lvl[u] - (1<= lvl[v]) 27 | u = up[u][i]; 28 | } 29 | if(u == v) 30 | return u; 31 | for(int i = LG; i >= 0; --i) { 32 | if(up[u][i] != up[v][i]) { 33 | u = up[u][i]; 34 | v = up[v][i]; 35 | } 36 | } 37 | return up[u][0]; 38 | } 39 | -------------------------------------------------------------------------------- /Maximum Spanning tree.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int tests; 4 | int n,m; 5 | int a[1<<20],b[1<<20],c[1<<20]; 6 | vector > v; 7 | int w[1<<20]; 8 | int ans; 9 | int get(int x) 10 | { 11 | if (x==w[x]) 12 | return x; 13 | return w[x]=get(w[x]); 14 | } 15 | void merge(int a,int b) 16 | { 17 | w[a]=b; 18 | } 19 | int main(){ 20 | ios_base::sync_with_stdio(0); 21 | cin>>tests; 22 | for (;tests;--tests) 23 | { 24 | cin>>n>>m; 25 | v.clear(); 26 | for (int i=1;i<=m;i++) 27 | { 28 | cin>>a[i]>>b[i]>>c[i]; 29 | v.push_back(make_pair(c[i],i)); 30 | } 31 | sort(v.begin(),v.end()); 32 | reverse(v.begin(),v.end()); 33 | for (int i=1;i<=n;i++) 34 | w[i]=i; 35 | 36 | ans=0; 37 | 38 | for (int i=0;i dis(n, 0); 6 | rep(k,1,n) { 7 | rep(i,0,n) { 8 | rep(j,0,n) { 9 | if(dis[i]+a[i][j] 4 | 5 | using namespace std; 6 | 7 | #define io ios_base::sync_with_stdio(false);cin.tie(NULL) 8 | #define all(v) v.begin(),v.end() 9 | #define pb push_back 10 | #define ins insert 11 | #define rep(i,j,k) for(ll i=j;i=k;--i) 13 | #define in freopen("input.txt","r",stdin); 14 | #define out freopen("output.txt","w",stdout); 15 | #define ff first 16 | #define ss second 17 | 18 | typedef long long int ll; 19 | typedef unsigned long long ull; 20 | typedef long double ld; 21 | typedef pair pll; 22 | 23 | const int N = 1e5 + 5; 24 | const int M = 1e5 + 5; 25 | const int inf = INT_MAX; 26 | const int MOD = 1e9 + 7; 27 | 28 | vector primes; 29 | ll nI[N],fI[N],fact[N]; 30 | 31 | ll max(ll a,ll b){return a>b?a:b;} 32 | ll min(ll a,ll b){return a>=1;}return r;} 36 | bool isPrime(ll n){if(n<=1)return 0;if(n<=3)return 1;if(n%2==0||n%3==0)return 0;for(ll i=5;i*i<=n;i+=6)if(n%i==0||n%(i+2)==0)return 0;return 1;} 37 | void find_primes(ll n=100000000){ll limit=floor(sqrt(n))+1;vector test;test.pb(2),primes.pb(2);for(ll i=3;in)hi=n;memset(p,true,sizeof(p));for(int i=0;i>=1;}return r;} 39 | ll modexpo(ll a,ll b){ll r=1;a%=MOD;while(b){if(b&1)r=(r*a)%MOD;a=(a*a)%MOD;b>>=1;}return r;} 40 | ll powm(ll a,ll b) {ll res=1LL;while(b) {if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;} 41 | ll nCr(ll n,ll r){ll res=1;if(r>n>>1)r=n-r;rep(i,0,r){res=(res*(n-i))%MOD;res=(res*modexpo(i+1,MOD-2))%MOD;}return res;} 42 | void binomial_pre(){nI[0]=nI[1]=fI[0]=fI[1]=fact[0]=1;rep(i,2,N)nI[i]=nI[MOD%i]*(MOD-MOD/i)%MOD;rep(i,2,N)fI[i]=(nI[i]*fI[i-1])%MOD;rep(i,1,N)fact[i]=(fact[i-1]*i)%MOD;} 43 | ll binomial(ll n,ll r){if(n>t; 50 | while(t--) { 51 | //code goes here 52 | 53 | } 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /PERL/0_or_[aaa].pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | #nuttela 3 | $text1 = "0123"; 4 | $text2 = "aaaaaaaabbbbbbb"; 5 | $text3 = "bcdefgh"; 6 | #$REGEX_GENERATED = /^0|[a]/; 7 | $fl1=0;$fl2=0;$fl3=0; 8 | if($text1 =~ /^0|[a]/){ 9 | $fl1=1; 10 | } 11 | if($text2 =~ /^0|[a]/){ 12 | $fl2=1; 13 | } 14 | if($text3 =~ /^0|[a]/){ 15 | $fl3=1; 16 | } 17 | print "$fl1\n$fl2\n$fl3\n"; 18 | 19 | 20 | #OUTPUT: 21 | #1 22 | #1 23 | #0 24 | -------------------------------------------------------------------------------- /PERL/MCA..bw.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | #nuttela 3 | $text1 = "MCAhellonbw"; 4 | $text2 = "MCAhellonbabb"; 5 | $text3 = "MChellonbw"; 6 | #$REGEX_GENERATED = /^(MCA)[a-z]*(bw)$/; 7 | $fl1=0;$fl2=0;$fl3=0; 8 | if($text1 =~ /^(MCA)[a-z]*(bw)$/){ 9 | $fl1=1; 10 | } 11 | if($text2 =~ /^(MCA)[a-z]*(bw)$/){ 12 | $fl2=1; 13 | } 14 | if($text3 =~ /^(MCA)[a-z]*(bw)$/){ 15 | $fl3=1; 16 | } 17 | print "$fl1\n$fl2\n$fl3\n"; 18 | 19 | 20 | #OUTPUT: 21 | #1 22 | #0 23 | #0 24 | -------------------------------------------------------------------------------- /PERL/online_shop.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 46 | Online Shopping 47 | 48 | 49 |

Online Shop

50 | 51 |
52 |
    53 |
  • 54 | Jeans 55 |
      56 |
    • Denim
    • 57 |
    • Pepe
    • 58 |
    • Levi's
    • 59 |
    60 |
  • 61 |
  • 62 | T-shirts 63 |
      64 |
    • Allen Solly
    • 65 |
    • Reebok
    • 66 |
    • XYZ
    • 67 |
    68 |
  • 69 |
70 |

71 |
72 | 73 |
74 |
    75 |
  • Addidas
  • 76 |
  • Reebok
  • 77 |
  • Hush-Puppies
  • 78 |
79 |
80 | 81 | 82 | 83 | -------------------------------------------------------------------------------- /PERL/vowelcount.pl: -------------------------------------------------------------------------------- 1 | #!/usr/bin/perl 2 | #nuttela 3 | $text = "abcdefghijklmnopqrstuvwxyz"; 4 | $cnt=0; 5 | foreach $v (split //, $text) 6 | { 7 | if($v =~ /[aeiou]/g){ 8 | $cnt++; 9 | } 10 | } 11 | print $cnt; 12 | 13 | 14 | #OUTPUT: 15 | #5 16 | -------------------------------------------------------------------------------- /Postorder, Preorder, Inorder.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | void postorder(int a[], vector &v, int n, int index) 5 | { 6 | if(index >= n) 7 | return; 8 | postorder(a, v, n, 2 * index + 1); 9 | postorder(a, v, n, 2 * index + 2); 10 | v.push_back(a[index]); 11 | } 12 | void inorder(int a[], vector &v, int n, int index) 13 | { 14 | if(index >= n) 15 | return; 16 | inorder(a, v, n, 2 * index + 1); 17 | v.push_back(a[index]); 18 | inorder(a, v, n, 2 * index + 2); 19 | 20 | } 21 | void preorder(int a[], vector &v, int n, int index) 22 | { 23 | if(index >= n) 24 | return; 25 | v.push_back(a[index]); 26 | preorder(a, v, n, 2 * index + 1); 27 | preorder(a, v, n, 2 * index + 2); 28 | } 29 | 30 | int32_t main() 31 | { 32 | ios_base::sync_with_stdio(false); 33 | cin.tie(NULL); 34 | int n=7; 35 | 36 | int a[]={ 5, 6, 7, 8, 9, 10, 11}; 37 | 38 | cout<<"Original Series"< v; 47 | 48 | inorder(a, v, n, 0); 49 | 50 | cout<<"INORDER :"<<'\n'; 51 | 52 | for (int i = 0; i < n; ++i) 53 | { 54 | cout< 2 | 3 | 4 | using namespace std; 5 | 6 | const int INF = INT_MAX; 7 | 8 | class edge{ public: int src,des,weight; edge(){}edge(int s,int d,int w): src(s),des(d),weight(w){}}; 9 | class compare { public: bool operator()(const edge &a,const edge &b){ return a.weight < b.weight; }}; 10 | 11 | vector primsAlgorithm(vector>> graph,edge minEdge){ 12 | vectorspanningTree; 13 | priority_queue,compare> Q; 14 | while(spanningTree.size() == graph.size()-1){ 15 | spanningTree.push_back(minEdge); 16 | for(list>::iterator it = graph[minEdge.src].begin();it!=graph[minEdge.src].end();it++){ 17 | Q.push(edge(minEdge.src,it->first,it->second)); 18 | } 19 | for(list>::iterator it = graph[minEdge.des].begin();it!=graph[minEdge.des].end();it++){ 20 | Q.push(edge(minEdge.des,it->first,it->second)); 21 | } 22 | minEdge = Q.top(); Q.pop(); 23 | } 24 | return spanningTree; 25 | } 26 | 27 | int32_t main(){ 28 | vector>>graph; 29 | int v,e,src,des,weight; 30 | cin >> v >> e; 31 | graph.resize(v); 32 | edge minEdge; 33 | minEdge.weight = INF; 34 | while(e--){ 35 | cin >> src >> des >> weight; 36 | graph[src].push_back(make_pair(des,weight)); 37 | graph[des].push_back(make_pair(src,weight)); 38 | if(weight < minEdge.weight){ 39 | minEdge.src = src, minEdge.des = des, minEdge.weight = weight; 40 | } 41 | } 42 | vector spanningTree = primsAlgorithm(graph,minEdge); 43 | for(edge x : spanningTree){ 44 | cout << x.src << " " << x.des << " " << x.weight << endl; 45 | } 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /Priority Queue.cpp: -------------------------------------------------------------------------------- 1 | priority_queue, greater> Q; 2 | //to input elements in a non-increasing order 3 | 4 | priority_queue Q; 5 | //to input elements in a non-decreasing order 6 | 7 | Q.push(X); 8 | //insert element 9 | 10 | int32_t main() 11 | { 12 | for (int i = 0; i < N; i++) 13 | { 14 | cin >> X; 15 | Q.push(X); 16 | cout << Q.top() << " "; 17 | } 18 | 19 | return 0; 20 | } 21 | 22 | /* 23 | ------------------------------------------------------------------------------------------------------------------------- 24 | 25 | Problems : https://www.codechef.com/problems/TSECJ05 26 | Solution : https://www.codechef.com/viewsolution/23672310 27 | 28 | Problem : https://www.codechef.com/problems/PEWDSVTS 29 | Solution : https://www.codechef.com/viewsolution/24063781 30 | 31 | Applications of PQ are imp! 32 | */ 33 | -------------------------------------------------------------------------------- /Prufer to Tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Prufer Code to Tree (or a Connected Graph :) ) 3 | Complexity: O(VlogV) 4 | */ 5 | 6 | vector> pruferCodeToTree(vector &pruferCode) { 7 | // Stores number count of nodes in the prufer code 8 | unordered_map nodeCount; 9 | 10 | // Set of integers absent in prufer code. They are the leaves 11 | set leaves; 12 | 13 | int len = pruferCode.size(); 14 | int node = len + 2; 15 | 16 | // Count frequency of nodes 17 | for ( int i = 0; i < len; i++ ) { 18 | int t = pruferCode[i]; 19 | nodeCount[t]++; 20 | } 21 | 22 | // Find the absent nodes 23 | for ( int i = 1; i <= node; i++ ) { 24 | if ( nodeCount.find ( i ) == nodeCount.end() ) leaves.insert ( i ); 25 | } 26 | 27 | vector> edges; 28 | /*Connect Edges*/ 29 | for ( int i = 0; i < len; i++ ){ 30 | int a = pruferCode[i]; // First node 31 | 32 | //Find the smallest number which is not present in prufer code now 33 | int b = *leaves.begin(); // the leaf 34 | 35 | edges.push_back({a,b}); // Edge of the tree 36 | 37 | leaves.erase ( b ); // Remove from absent list 38 | nodeCount[a]--; // Remove from prufer code 39 | if ( nodeCount[a] == 0 ) leaves.insert ( a ); // If a becomes absent 40 | } 41 | 42 | // The final edge 43 | edges.push_back({*leaves.begin(), *leaves.rbegin()}); 44 | return edges; 45 | } 46 | -------------------------------------------------------------------------------- /RapyutaTest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() { 6 | int n; 7 | cin >> n; 8 | int arr[n + 1]; 9 | for (int i = 1; i <= n; ++i) { 10 | cin >> arr[i]; 11 | } 12 | int dp[n + 1][n + 1]; 13 | memset(dp, sizeof(dp), 0); 14 | int res = INT_MIN; 15 | dp[1][0] = 0; 16 | dp[1][1] = arr[1]; 17 | for (int i = 2; i <= n; ++i) { 18 | for (int j = 0; j <= i; ++j) { 19 | if(j == 0){ 20 | dp[i][j] = 0; 21 | continue; 22 | } 23 | if(j == i) { 24 | dp[i][j] = arr[i] * j + dp[i - 1][j - 1]; 25 | } 26 | else { 27 | dp[i][j] = max(dp[i - 1][j], arr[i] * j + dp[i - 1][j - 1]); 28 | } 29 | res = max(res, dp[i][j]); 30 | } 31 | } 32 | cout << res; 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /SPOJ/ANDROUND.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | status : WA 3 | 4 | Link to Problem : https://www.spoj.com/problems/ANDROUND/ 5 | 6 | */ 7 | 8 | 9 | #pragma Gpref optimize("O3") 10 | //#pragma Gpref optimize("Ofast") 11 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 12 | //#pragma Gpref optimize("unroll-loops") 13 | #include 14 | #include 15 | #include 16 | using namespace __gnu_pbds; 17 | using namespace std; 18 | #define int long long 19 | #define ld long double 20 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 21 | #define TC int t; cin >> t; while(t--) 22 | #define rep(i,j,k) for(int i=j; i=k; --i) 24 | #define pb push_back 25 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 26 | #define F first 27 | #define S second 28 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 29 | const int MOD = (int)1e9+7; 30 | const int MAX = 1e5+5; 31 | typedef pair prs; 32 | 33 | int SET = 1073741823; 34 | int n,k,q,rem,cnt=0,ll,rr; 35 | int T[4*MAX]; 36 | vector arr; 37 | vector parent,size; 38 | 39 | /* 40 | 001 101 41 | 010 011 42 | 001 010 011 43 | 1 2 3 = 44 | 45 | void pre(int n){ 46 | parent.resize(n+1,0);size.resize(n+1,0); 47 | rep(i,1,n+1){ 48 | parent[i] = i; 49 | size[i] = 1; 50 | } 51 | } 52 | int find_parent(int v){ 53 | if (v == parent[v]) 54 | return v; 55 | return parent[v] = find_parent(parent[v]); 56 | } 57 | 58 | void union_set(int a,int b){ 59 | a = find_parent(a); 60 | b = find_parent(b); 61 | if (a != b) { 62 | if (size[a] < size[b]) 63 | swap(a, b); 64 | parent[b] = a; 65 | size[a] += size[b]; 66 | } 67 | } 68 | */ 69 | void build(int node,int s,int e){ 70 | if(s==e){ 71 | T[node] = arr[s]; 72 | return; 73 | } 74 | int m=(s+e)>>1; 75 | build(node<<1,s,m); 76 | build(node<<1|1,m+1,e); 77 | //cout << (T[node<<1] & T[node<<1|1]) << endl; 78 | T[node]=T[node<<1] & T[node<<1|1]; 79 | } 80 | int query(int node,int s,int e,int l,int r){ 81 | if(er) return SET; //no overlap 82 | if(l<=s && r>=e) return T[node]; //total overlap 83 | int mid = (s+e)>>1; 84 | return ((query(node<<1,s,mid,l,r)) & (query(node<<1|1,mid+1,e,l,r))); 85 | } 86 | /*void upd(int node,int s,int e,int p,int new_val){ 87 | if(s == e)T[node] +=new_val; 88 | else 89 | { 90 | int mid = (s+e)>>1; 91 | if(p <= mid) 92 | { 93 | upd(node<<1, s, mid, p, new_val); 94 | } 95 | else 96 | { 97 | upd(node<<1|1, mid+1, e, p, new_val); 98 | } 99 | T[node] = (T[node<<1] & T[node<<1|1]); 100 | } 101 | }*/ 102 | int32_t main(){ 103 | //freopen("ic1.txt", "r", stdin); 104 | //freopen("oc1.txt", "w", stdout); 105 | IOS; 106 | TC{ 107 | int base = 0; 108 | int n,k; 109 | cin >> n >> k; 110 | arr.resize(n+2,0); 111 | vector a(n),v; 112 | rep(i,0,n)cin>>a[i]; 113 | int left = a[n-1], right = a[0]; 114 | 115 | // 1 2 3 4 (1&2) 116 | int j=1; 117 | rep(i,0,min(k-1,n-1)){ 118 | //cout << j << ' '; 119 | if(right==0)break; 120 | right = right & (a[j++]); 121 | } 122 | 123 | int hi;j=n-2; 124 | if(k>n)hi=n-1;else hi=k-1; 125 | rep(i,0,hi){ 126 | if(left==0)break; 127 | left = left & (a[j--]); 128 | } 129 | 130 | //cout << left << right; 131 | arr[0]=left; 132 | arr[n+1]=right; 133 | rep(i,1,n+1)arr[i]=a[i-1]; 134 | 135 | //rep(i,0,n+2)cout << arr[i] << ' ';cout<> N >> q; 217 | 218 | rep(i,0,N){ 219 | rep(j,0,N){ 220 | cin >> mat[i][j]; 221 | } 222 | } 223 | char l;int r; 224 | while(q--){ 225 | cin >> l; 226 | if(l=='L'){ cin>>r; 227 | rep(i,0,r){ 228 | 229 | 230 | 231 | for (int x = 0; x < N / 2; x++) 232 | { 233 | // Consider elements in group of 4 in 234 | // current square 235 | for (int y = x; y < N-x-1; y++) 236 | { 237 | // store current cell in temp variable 238 | int temp = mat[x][y]; 239 | 240 | // move values from right to top 241 | mat[x][y] = mat[y][N-1-x]; 242 | 243 | // move values from bottom to right 244 | mat[y][N-1-x] = mat[N-1-x][N-1-y]; 245 | 246 | // move values from left to bottom 247 | mat[N-1-x][N-1-y] = mat[N-1-y][x]; 248 | 249 | // assign temp to left 250 | mat[N-1-y][x] = temp; 251 | } 252 | } 253 | 254 | 255 | } 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | } 264 | else {cin>>r; 265 | rep(i,0,r){ 266 | 267 | 268 | // Traverse each cycle 269 | for (int i = 0; i < N / 2; i++) { 270 | for (int j = i; j < N - i - 1; j++) { 271 | 272 | // Swap elements of each cycle 273 | // in clockwise direction 274 | int temp = mat[i][j]; 275 | mat[i][j] = mat[N - 1 - j][i]; 276 | mat[N - 1 - j][i] = mat[N - 1 - i][N - 1 - j]; 277 | mat[N - 1 - i][N - 1 - j] = mat[j][N - 1 - i]; 278 | mat[j][N - 1 - i] = temp; 279 | } 280 | } 281 | 282 | 283 | 284 | 285 | }} 286 | 287 | 288 | } 289 | for (int i=0; i> a[i][j]; 307 | } 308 | } 309 | int fl=0; 310 | rep(i,0,n){ 311 | rep(j,0,m){ 312 | if(a[i][j]){ 313 | if((a[i-1][j]==1&&a[i-1][j-1]==1&&a[i][j-1]==1) || (a[i+1][j]==1&&a[i+1][j-1]==1&&a[i][j-1]==1) || (a[i-1][j+1]==1&&a[i][j+1]==1&&a[i-1][j]==1) || 314 | (a[i+1][j]==1&&a[i][j+1]==1&&a[i+1][j+1]==1)){ 315 | continue; 316 | }else {fl=1;break;} 317 | } 318 | } 319 | } 320 | set vec; 321 | rep(i,0,n){ 322 | rep(j,0,m){ 323 | if(a[i][j]==1){ 324 | bool upleft = (a[i-1][j]==1&&a[i-1][j-1]==1&&a[i][j-1]==1); 325 | bool upright = (a[i+1][j]==1&&a[i+1][j-1]==1&&a[i][j-1]==1); 326 | bool downleft = (a[i-1][j+1]==1&&a[i][j+1]==1&&a[i-1][j]==1); 327 | bool downright = (a[i+1][j]==1&&a[i][j+1]==1&&a[i+1][j+1]==1); 328 | //cout << upleft << upright << downleft << downright << endl; 329 | if (upleft){int x=i-1,y=j-1;vec.insert({x+1,y+1});} //upleft 330 | else if (upright){int x=i-1,y=j;vec.insert({x+1,y+1});} 331 | else if (downleft){int x=i,y=j-1;vec.insert({x+1,y+1});} 332 | else if (downright){int x=i,y=j;vec.insert({x+1,y+1});} 333 | } 334 | } 335 | } 336 | 337 | if(fl)cout<<-1< 373 | using namespace std; 374 | 375 | int main() 376 | { 377 | freopen("i3.txt", "w", stdout); 378 | 379 | srand(time(NULL)); 380 | 381 | std::random_device rd; 382 | std::mt19937 eng(rd()); 383 | std::uniform_int_distribution<> distr(1, 100); 384 | static const char generator[] = "abcdefghijklmnopqrstuvwxyz"; 385 | static const char leet[] = "12"; 386 | 387 | int tc = 10; 388 | 389 | cout << tc << '\n'; 390 | 391 | while(tc--) 392 | { 393 | //10000 + rand() % (( 100000 + 1 ) - 10000); 394 | //cout << len << endl; 395 | 396 | //struct timeval time; 397 | ///gettimeofday(&time,NULL); 398 | //srand((time.tv_sec * 1000) + (time.tv_usec / 1000)); 399 | 400 | const int maxN = 100000; 401 | const int N = rand() % maxN + 1; 402 | 403 | cout << N << endl; 404 | int x = -100000000, y = 10000000; 405 | for (int i = 0; i < N; ++i) 406 | { 407 | cout << x + rand() % (( y + 1 ) - (x))<< ' '; 408 | }cout << endl; 409 | 410 | vector nodeIds; 411 | for (int i = 1; i <= N; i++) 412 | { 413 | nodeIds.push_back(i); 414 | } 415 | random_shuffle(nodeIds.begin(), nodeIds.end()); 416 | 417 | vector usedNodeIds = { nodeIds.back() }; 418 | nodeIds.pop_back(); 419 | 420 | for (int i = 0; i < N - 1; i++) 421 | { 422 | const int newNodeId = nodeIds.back(); 423 | const int oldNodeId = usedNodeIds[rand() % usedNodeIds.size()]; 424 | 425 | cout << newNodeId << " " << oldNodeId << endl; 426 | 427 | usedNodeIds.push_back(newNodeId); 428 | nodeIds.pop_back(); 429 | } 430 | 431 | 432 | /*char s[len]; 433 | for (int i = 0; i < len; ++i) s[i] = generator[rand() % (sizeof(generator) - 1)]; 434 | 435 | for (int i = 0; i < len; ++i)cout << s[i]; 436 | cout << '\n'; 437 | 438 | int query = 100000;//10000 + rand() % (( 100000 + 1 ) - 10000); 439 | //min + rand() % (( max + 1 ) - min); 440 | 441 | cout << query << '\n'; 442 | while(query--) 443 | { 444 | char task = leet[rand() % (sizeof(leet) - 1)]; 445 | 446 | if(task == '1') 447 | { 448 | int l,r; 449 | l = 1 + rand() % (( N + 1 ) - 1); 450 | r = x + rand() % (( y + 1 ) - (x)); 451 | cout << 1 << ' ' << l << ' ' << r << '\n'; 452 | } 453 | 454 | else 455 | { 456 | int p; 457 | p = 1 + rand() % (( N + 1 ) - 1); 458 | cout << 2 << ' ' << p << '\n'; 459 | 460 | } 461 | } 462 | 463 | } 464 | return 0; 465 | } 466 | 467 | 468 | 469 | 470 | 12 1 2 3 4 6 12 4-2 2 471 | 472 | 24 1 2 3 4 6 12 24 5-2 = 3 473 | 474 | 475 | 1 2 3 4 6 12 476 | 1 2 3 4 6 8 12 24 477 | 478 | 1 2 3 5 10 15 30 479 | 1 3 9 18 480 | 481 | 14 28 482 | 483 | 1 2 7 14 484 | 1 2 7 14 28 485 | 486 | odd even = even 487 | even even = even 488 | 489 | 490 | 3 4 7 3 4 7 491 | 0 1 2 3 4 5 492 | 0 1 2 0 1 2 493 | 494 | 495 | 325 265 76 325 265 76 496 | 0 1 2 3 4 5 497 | 498 | 499 | 1 1 2 2 3 4 500 | 501 | 502 | 503 | 11 504 | 12 505 | 13 506 | 1 4 1 4 9 507 | 508 | 1 - 13 509 | 4 - 24 510 | 9 - 5 511 | 512 | 1 1 2 2 513 | 514 | 1-12 515 | 2-34 516 | 517 | 1 2 3 2 4 1 5 518 | 519 | 1 - 16 520 | 2 - 24 521 | 3 - 3 522 | 4 - 5 523 | 5 - 7 524 | 525 | 1 2 4 6 526 | 527 | 0 1 2 4 528 | 529 | 1. Toposorting 530 | 2. SQRT 531 | */ 532 | -------------------------------------------------------------------------------- /SPOJ/ANT.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Connected Points - Union Find 3 | Problem link : https://www.spoj.com/problems/ANTTT/ 4 | Any IDEA why WA ? 5 | 6 | 7 | */ 8 | 9 | 10 | 11 | #include 12 | #define int long long int 13 | using namespace std; 14 | 15 | 16 | vector parent, size; 17 | // Lines intersection code --> https://www.geeksforgeeks.org/check-if-two-given-line-segments-intersect/ 18 | struct Point 19 | { 20 | int x; 21 | int y; 22 | }; 23 | 24 | bool onSegment(Point p, Point q, Point r) 25 | { 26 | if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) && 27 | q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y)) 28 | return true; 29 | 30 | return false; 31 | } 32 | 33 | int orientation(Point p, Point q, Point r) 34 | { 35 | int val = (q.y - p.y) * (r.x - q.x) - 36 | (q.x - p.x) * (r.y - q.y); 37 | 38 | if (val == 0) return 0; // colinear 39 | 40 | return (val > 0)? 1: 2; // clock or counterclock wise 41 | } 42 | bool doIntersect(Point p1, Point q1, Point p2, Point q2) 43 | { 44 | // Find the four orientations needed for general and 45 | // special cases 46 | int o1 = orientation(p1, q1, p2); 47 | int o2 = orientation(p1, q1, q2); 48 | int o3 = orientation(p2, q2, p1); 49 | int o4 = orientation(p2, q2, q1); 50 | 51 | // General case 52 | if (o1 != o2 && o3 != o4) 53 | return true; 54 | 55 | // Special Cases 56 | // p1, q1 and p2 are colinear and p2 lies on segment p1q1 57 | if (o1 == 0 && onSegment(p1, p2, q1)) return true; 58 | 59 | // p1, q1 and q2 are colinear and q2 lies on segment p1q1 60 | if (o2 == 0 && onSegment(p1, q2, q1)) return true; 61 | 62 | // p2, q2 and p1 are colinear and p1 lies on segment p2q2 63 | if (o3 == 0 && onSegment(p2, p1, q2)) return true; 64 | 65 | // p2, q2 and q1 are colinear and q1 lies on segment p2q2 66 | if (o4 == 0 && onSegment(p2, q1, q2)) return true; 67 | 68 | return false; // Doesn't fall in any of the above cases 69 | } 70 | 71 | int getid(int x){ 72 | if(x == parent[x])return x; 73 | else return parent[x] = getid(parent[x]); 74 | } 75 | 76 | void unite(int x, int y){ 77 | x = getid(x); 78 | y = getid(y); 79 | parent[y] = x; 80 | size[x] += size[y]; 81 | } 82 | int32_t main(){ 83 | int t; 84 | cin >> t; 85 | while(t--){ 86 | int n,m,n1,m1; 87 | int j=0; 88 | cin >> n >> m; 89 | n1=n; 90 | m1=m; 91 | int x1[n],x2[n],y1[n],y2[n]; 92 | while(n1--){ 93 | cin >> x1[j] >> y1[j] >> x2[j] >> y2[j]; 94 | ++j; 95 | } 96 | //process 97 | parent.resize(n+1,0); 98 | size.resize(n+1,0); 99 | for (int i = 1; i <=n; ++i) 100 | { 101 | parent[i] = i; 102 | size[i] = 1; 103 | } 104 | for (int i = 0; i < n-1; ++i) 105 | { 106 | struct Point p1 = {x1[i],y1[i]}; // p1---p2 107 | struct Point q1 = {x2[i],y2[i]}; // p1---p2 108 | struct Point p2 = {x1[i+1],y1[i+1]}; //q1----q2 109 | struct Point q2 = {x2[i+1],y2[i+1]}; //q1----q2 110 | if(doIntersect(p1, q1, p2, q2)){ 111 | unite(i+1,i+2); 112 | } 113 | } 114 | while(m--){ 115 | int a,b; 116 | cin >> a >> b; 117 | a = getid(a); 118 | b = getid(b); 119 | cout << ((a==b)?"YES":"NO") << '\n'; 120 | } 121 | } 122 | return 0; 123 | } 124 | -------------------------------------------------------------------------------- /SPOJ/DISQUERY.cpp: -------------------------------------------------------------------------------- 1 | #pragma Gpref optimize("O3") 2 | //#pragma Gpref optimize("Ofast") 3 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 4 | //#pragma Gpref optimize("unroll-loops") 5 | #include 6 | #include 7 | #include 8 | #define int long long 9 | #define ld long double 10 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 11 | #define TC int t; cin >> t; while(t--) 12 | #define rep(i,j,k) for(int i=j; i=k; --i) 14 | #define pb push_back 15 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 16 | #define F first 17 | #define S second 18 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 19 | 20 | using namespace __gnu_pbds; 21 | using namespace std; 22 | 23 | const int MOD = (int)1e9+7; 24 | int timer; 25 | const int MAX = 1e5+5; 26 | typedef pair pairs; 27 | const int LG = ceil(log2(MAX)); 28 | 29 | vector> up(MAX, vector(LG+1)); 30 | vector tin(MAX),tout(MAX),level(MAX); 31 | vector G[MAX]; 32 | map,int> weights; 33 | void dfs(int u,int par, int height=0){ 34 | level[u] = height; 35 | tin[u] = ++timer; 36 | up[u][0] = par; 37 | for(int i = 1; i <= LG; i++){ 38 | up[u][i]=up[up[u][i-1]][i-1]; 39 | } 40 | for(auto x : G[u]){ 41 | if(x!=par)dfs(x,u, height+1); 42 | } 43 | tout[u] = ++timer; 44 | } 45 | bool is_ancestor(int u, int v) 46 | { 47 | return tin[u] <= tin[v] && tout[u] >= tout[v]; 48 | } 49 | 50 | int lca(int u, int v) 51 | { 52 | if (is_ancestor(u, v)) 53 | return u; 54 | if (is_ancestor(v, u)) 55 | return v; 56 | for (int i = LG; i >= 0; --i) { 57 | if (!is_ancestor(up[u][i], v)) 58 | u = up[u][i]; 59 | } 60 | return up[u][0]; 61 | } 62 | 63 | int lcamin(int a, int b){ 64 | //ay)swap(x,y); 74 | mn = min(mn,weights[{x,y}]); 75 | down = up[down][0]; 76 | } 77 | return mn; 78 | } 79 | int lcamax(int a, int b){ 80 | if(level[a]y)swap(x,y); 87 | mn = max(mn,weights[{x,y}]); 88 | down = up[down][0]; 89 | } 90 | return mn; 91 | } 92 | 93 | int32_t main(){ 94 | IOS; 95 | int n,u,v,q,w; 96 | cin >> n; 97 | timer=0; 98 | rep(i,1,n){ 99 | cin >> u >> v >> w; 100 | G[u].pb(v); 101 | G[v].pb(u); 102 | if(u>v)swap(u,v); 103 | weights[{u,v}] = w; 104 | //cout << weights[{u,v}] << endl; 105 | } 106 | dfs(1,1); 107 | cin >> q; 108 | while(q--){ 109 | int l,r; 110 | cin >> l >> r; 111 | int rt = lca(l,r); 112 | //cout << rt << ' '; 113 | int lcamin1 = lcamin(l,rt); 114 | int lcamax1 = lcamax(l,rt); 115 | int lcamin2 = lcamin(r,rt); 116 | int lcamax2 = lcamax(r,rt); 117 | //cout << lcamin1 <<' '<< lcamin2 <<' '<< lcamax1 <<' '<< lcamax2 << endl; 118 | cout << min(lcamin1,lcamin2) << ' ' << max(lcamax1,lcamax2) << '\n'; 119 | } 120 | return 0; 121 | } 122 | -------------------------------------------------------------------------------- /SPOJ/GSS1.cpp: -------------------------------------------------------------------------------- 1 | // https://e-maxx.ru/algo/segment_tree 2 | #pragma Gpref optimize("O3") 3 | //#pragma Gpref optimize("Ofast") 4 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 5 | //#pragma Gpref optimize("unroll-loops") 6 | #include 7 | #include 8 | #include 9 | using namespace __gnu_pbds; 10 | using namespace std; 11 | #define int long long 12 | #define ld long double 13 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 14 | #define TC int t; cin >> t; while(t--) 15 | #define rep(i,j,k) for(int i=j; i=k; --i) 17 | #define pb push_back 18 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 19 | #define F first 20 | #define S second 21 | #define all(x) x.begin(), x.end() 22 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 23 | const int MOD = (int)1e9+7; 24 | const int MAX = 5e4+4; 25 | typedef pair prs; 26 | 27 | typedef struct data{ 28 | int sum, pref, suff, ans; 29 | }data; 30 | 31 | inline data combine(data l, data r){ 32 | data d; 33 | d.sum = l.sum + r.sum; 34 | d.pref = max(l.pref, l.sum + r.pref); 35 | d.suff = max(r.suff, r.sum + l.suff); 36 | d.ans = max({l.ans,r.ans,l.suff+r.pref}); 37 | return d; 38 | } 39 | 40 | inline data mkdata(int v){ 41 | data d; 42 | d.sum = d.pref = d.suff = d.ans = v; 43 | return d; 44 | } 45 | 46 | data T[4*MAX];vector arr; 47 | 48 | void build(int node, int s, int e){ 49 | if(s==e){ 50 | T[node]=mkdata(arr[s]); 51 | return; 52 | } 53 | int mid=(s+e)>>1; 54 | build(node<<1,s,mid);build(node<<1|1,mid+1,e); 55 | T[node]=combine(T[node<<1],T[node<<1|1]); 56 | } 57 | 58 | data query(int node, int l, int r, int p, int q) { 59 | if(l >= p && r <= q) 60 | return T[node]; 61 | int m = (l+r)>>1; 62 | if(q <= m) 63 | return query(node<<1, l, m, p, q); 64 | else if(p > m) 65 | return query(node<<1|1, m+1, r, p, q); 66 | data d1 = query(node<<1, l, m, p, q), d2 = query(node<<1|1, m+1, r, p, q); 67 | return combine(d1, d2); 68 | } 69 | 70 | int32_t main() { 71 | IOS; 72 | int n,q; 73 | scanf("%lld",&n); 74 | arr.resize(n,0); 75 | rep(i,0,n)scanf("%lld",&arr[i]); 76 | build(1,0,n-1); 77 | scanf("%lld",&q); 78 | while(q--) 79 | { 80 | int l,r; 81 | scanf("%lld%lld",&l,&r); 82 | printf("%lld\n",query(1,0,n-1,l-1,r-1).ans); 83 | } 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /SPOJ/ORDERSET.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Segment Trees (Hard) 4 | Using Policy based data structures... 5 | https://www.spoj.com/problems/ORDERSET 6 | 7 | */ 8 | 9 | 10 | 11 | #pragma Gpref optimize("O3") 12 | //#pragma Gpref optimize("Ofast") 13 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 14 | //#pragma Gpref optimize("unroll-loops") 15 | #include 16 | #include 17 | #include 18 | using namespace __gnu_pbds; 19 | using namespace std; 20 | #define int long long 21 | #define ld long double 22 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 23 | #define TC int t; cin >> t; while(t--) 24 | #define rep(i,j,k) for(int i=j; i=k; --i) 26 | #define pb push_back 27 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 28 | #define F first 29 | #define S second 30 | #define ordered_set tree, rb_tree_tag,tree_order_statistics_node_update> 31 | const int MOD = (int)1e9+7; 32 | const int MAX = 1e5+1;; 33 | typedef pair prs; 34 | 35 | 36 | int n,k,q,rem,cnt=0,ll,rr; 37 | int T[4*MAX]; 38 | vector arr; 39 | vector parent,size; 40 | 41 | /*void pre(int n){ 42 | parent.resize(n+1,0);size.resize(n+1,0); 43 | rep(i,1,n+1){ 44 | parent[i] = i; 45 | size[i] = 1; 46 | } 47 | } 48 | int find_parent(int v){ 49 | if (v == parent[v]) 50 | return v; 51 | return parent[v] = find_parent(parent[v]); 52 | } 53 | 54 | void union_set(int a,int b){ 55 | a = find_parent(a); 56 | b = find_parent(b); 57 | if (a != b) { 58 | if (size[a] < size[b]) 59 | swap(a, b); 60 | parent[b] = a; 61 | size[a] += size[b]; 62 | } 63 | } 64 | 65 | void build(int node,int s,int e){ 66 | if(s==e){ 67 | T[node] = arr[s]%k; 68 | return; 69 | } 70 | int m=(s+e)>>1; 71 | build(node<<1,s,m); 72 | build(node<<1|1,m+1,e); 73 | if(T[node<<1]%k==0)ll=; 74 | if(T[node<<1|1]%k==0)rr=1; 75 | T[node]=ll+rr; 76 | } 77 | int query(int node,int s,int e,int l,int r){ 78 | if(er) return 0; //no overlap 79 | if(l<=s && r>=e) return T[node]; //total overlap 80 | int mid = (s+e)>>1; 81 | int lft = ((query(node<<1,s,mid,l,r))); 82 | int ryt = ((query(node<<1|1,mid+1,e,l,r))); 83 | if(lft==rem)cnt++; 84 | return (lft+ryt); 85 | } 86 | void upd(int node,int s,int e,int p,int new_val){ 87 | if(s == e)T[node] +=new_val; 88 | else 89 | { 90 | int mid = (s + e)/2; 91 | if(p <= mid) 92 | { 93 | upd(2*node, s, mid, p, new_val); 94 | } 95 | else 96 | { 97 | upd(2*node+1, mid+1, e, p, new_val); 98 | } 99 | T[node] = (T[2*node]+T[2*node+1]); 100 | } 101 | }*/ 102 | int32_t main(){ 103 | //freopen("ic1.txt", "r", stdin); 104 | //freopen("oc1.txt", "w", stdout); 105 | IOS; 106 | int q; 107 | cin >> q; 108 | ordered_set st; 109 | while(q--){ 110 | char ch;int x; 111 | cin >> ch >> x; 112 | if(ch == 'I'){ 113 | st.insert(x); 114 | } 115 | if(ch == 'D'){ 116 | if(st.find(x)!=st.end())st.erase(st.find(x)); 117 | } 118 | if(ch == 'K'){ 119 | if(x>st.size())cout<<"invalid"<> N >> q; 187 | 188 | rep(i,0,N){ 189 | rep(j,0,N){ 190 | cin >> mat[i][j]; 191 | } 192 | } 193 | char l;int r; 194 | while(q--){ 195 | cin >> l; 196 | if(l=='L'){ cin>>r; 197 | rep(i,0,r){ 198 | 199 | 200 | 201 | for (int x = 0; x < N / 2; x++) 202 | { 203 | // Consider elements in group of 4 in 204 | // current square 205 | for (int y = x; y < N-x-1; y++) 206 | { 207 | // store current cell in temp variable 208 | int temp = mat[x][y]; 209 | 210 | // move values from right to top 211 | mat[x][y] = mat[y][N-1-x]; 212 | 213 | // move values from bottom to right 214 | mat[y][N-1-x] = mat[N-1-x][N-1-y]; 215 | 216 | // move values from left to bottom 217 | mat[N-1-x][N-1-y] = mat[N-1-y][x]; 218 | 219 | // assign temp to left 220 | mat[N-1-y][x] = temp; 221 | } 222 | } 223 | 224 | 225 | } 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | } 234 | else {cin>>r; 235 | rep(i,0,r){ 236 | 237 | 238 | // Traverse each cycle 239 | for (int i = 0; i < N / 2; i++) { 240 | for (int j = i; j < N - i - 1; j++) { 241 | 242 | // Swap elements of each cycle 243 | // in clockwise direction 244 | int temp = mat[i][j]; 245 | mat[i][j] = mat[N - 1 - j][i]; 246 | mat[N - 1 - j][i] = mat[N - 1 - i][N - 1 - j]; 247 | mat[N - 1 - i][N - 1 - j] = mat[j][N - 1 - i]; 248 | mat[j][N - 1 - i] = temp; 249 | } 250 | } 251 | 252 | 253 | 254 | 255 | }} 256 | 257 | 258 | } 259 | for (int i=0; i> a[i][j]; 277 | } 278 | } 279 | int fl=0; 280 | rep(i,0,n){ 281 | rep(j,0,m){ 282 | if(a[i][j]){ 283 | if((a[i-1][j]==1&&a[i-1][j-1]==1&&a[i][j-1]==1) || (a[i+1][j]==1&&a[i+1][j-1]==1&&a[i][j-1]==1) || (a[i-1][j+1]==1&&a[i][j+1]==1&&a[i-1][j]==1) || 284 | (a[i+1][j]==1&&a[i][j+1]==1&&a[i+1][j+1]==1)){ 285 | continue; 286 | }else {fl=1;break;} 287 | } 288 | } 289 | } 290 | set vec; 291 | rep(i,0,n){ 292 | rep(j,0,m){ 293 | if(a[i][j]==1){ 294 | bool upleft = (a[i-1][j]==1&&a[i-1][j-1]==1&&a[i][j-1]==1); 295 | bool upright = (a[i+1][j]==1&&a[i+1][j-1]==1&&a[i][j-1]==1); 296 | bool downleft = (a[i-1][j+1]==1&&a[i][j+1]==1&&a[i-1][j]==1); 297 | bool downright = (a[i+1][j]==1&&a[i][j+1]==1&&a[i+1][j+1]==1); 298 | //cout << upleft << upright << downleft << downright << endl; 299 | if (upleft){int x=i-1,y=j-1;vec.insert({x+1,y+1});} //upleft 300 | else if (upright){int x=i-1,y=j;vec.insert({x+1,y+1});} 301 | else if (downleft){int x=i,y=j-1;vec.insert({x+1,y+1});} 302 | else if (downright){int x=i,y=j;vec.insert({x+1,y+1});} 303 | } 304 | } 305 | } 306 | 307 | if(fl)cout<<-1< 343 | using namespace std; 344 | 345 | int main() 346 | { 347 | freopen("i3.txt", "w", stdout); 348 | 349 | srand(time(NULL)); 350 | 351 | std::random_device rd; 352 | std::mt19937 eng(rd()); 353 | std::uniform_int_distribution<> distr(1, 100); 354 | static const char generator[] = "abcdefghijklmnopqrstuvwxyz"; 355 | static const char leet[] = "12"; 356 | 357 | int tc = 10; 358 | 359 | cout << tc << '\n'; 360 | 361 | while(tc--) 362 | { 363 | //10000 + rand() % (( 100000 + 1 ) - 10000); 364 | //cout << len << endl; 365 | 366 | //struct timeval time; 367 | ///gettimeofday(&time,NULL); 368 | //srand((time.tv_sec * 1000) + (time.tv_usec / 1000)); 369 | 370 | const int maxN = 100000; 371 | const int N = rand() % maxN + 1; 372 | 373 | cout << N << endl; 374 | int x = -100000000, y = 10000000; 375 | for (int i = 0; i < N; ++i) 376 | { 377 | cout << x + rand() % (( y + 1 ) - (x))<< ' '; 378 | }cout << endl; 379 | 380 | vector nodeIds; 381 | for (int i = 1; i <= N; i++) 382 | { 383 | nodeIds.push_back(i); 384 | } 385 | random_shuffle(nodeIds.begin(), nodeIds.end()); 386 | 387 | vector usedNodeIds = { nodeIds.back() }; 388 | nodeIds.pop_back(); 389 | 390 | for (int i = 0; i < N - 1; i++) 391 | { 392 | const int newNodeId = nodeIds.back(); 393 | const int oldNodeId = usedNodeIds[rand() % usedNodeIds.size()]; 394 | 395 | cout << newNodeId << " " << oldNodeId << endl; 396 | 397 | usedNodeIds.push_back(newNodeId); 398 | nodeIds.pop_back(); 399 | } 400 | 401 | 402 | /*char s[len]; 403 | for (int i = 0; i < len; ++i) s[i] = generator[rand() % (sizeof(generator) - 1)]; 404 | 405 | for (int i = 0; i < len; ++i)cout << s[i]; 406 | cout << '\n'; 407 | 408 | int query = 100000;//10000 + rand() % (( 100000 + 1 ) - 10000); 409 | //min + rand() % (( max + 1 ) - min); 410 | 411 | cout << query << '\n'; 412 | while(query--) 413 | { 414 | char task = leet[rand() % (sizeof(leet) - 1)]; 415 | 416 | if(task == '1') 417 | { 418 | int l,r; 419 | l = 1 + rand() % (( N + 1 ) - 1); 420 | r = x + rand() % (( y + 1 ) - (x)); 421 | cout << 1 << ' ' << l << ' ' << r << '\n'; 422 | } 423 | 424 | else 425 | { 426 | int p; 427 | p = 1 + rand() % (( N + 1 ) - 1); 428 | cout << 2 << ' ' << p << '\n'; 429 | 430 | } 431 | } 432 | 433 | } 434 | return 0; 435 | } 436 | 437 | 438 | 439 | 440 | 12 1 2 3 4 6 12 4-2 2 441 | 442 | 24 1 2 3 4 6 12 24 5-2 = 3 443 | 444 | 445 | 1 2 3 4 6 12 446 | 1 2 3 4 6 8 12 24 447 | 448 | 1 2 3 5 10 15 30 449 | 1 3 9 18 450 | 451 | 14 28 452 | 453 | 1 2 7 14 454 | 1 2 7 14 28 455 | 456 | odd even = even 457 | even even = even 458 | 459 | 460 | 3 4 7 3 4 7 461 | 0 1 2 3 4 5 462 | 0 1 2 0 1 2 463 | 464 | 465 | 325 265 76 325 265 76 466 | 0 1 2 3 4 5 467 | 468 | 469 | 1 1 2 2 3 4 470 | 471 | 472 | 473 | 11 474 | 12 475 | 13 476 | 1 4 1 4 9 477 | 478 | 1 - 13 479 | 4 - 24 480 | 9 - 5 481 | 482 | 1 1 2 2 483 | 484 | 1-12 485 | 2-34 486 | 487 | 1 2 3 2 4 1 5 488 | 489 | 1 - 16 490 | 2 - 24 491 | 3 - 3 492 | 4 - 5 493 | 5 - 7 494 | 495 | 1 2 4 6 496 | 497 | 0 1 2 4 498 | 499 | 1. Toposorting 500 | 2. SQRT 501 | */ 502 | -------------------------------------------------------------------------------- /SQRT_DECOMPOSITION.cpp: -------------------------------------------------------------------------------- 1 | 2 | vector arr, input_arr,blocks; 3 | vector parent,size; 4 | int block_size; 5 | 6 | 7 | void SQRT_update(int index, int val){ 8 | int blk = index/block_size; 9 | blocks[blk] += (val - input_arr[index]); 10 | input_arr[index] = val; 11 | } 12 | 13 | int SQRT_query(int l, int r){ 14 | int sum = 0; 15 | while (l> n; 49 | arr.resize(n,0); 50 | input_arr.resize(n,0); 51 | rep(i,0,n)cin>>input_arr[i]; 52 | build_SQRT_DECOMPED_TREE(n); 53 | cin >> q; 54 | while(q--){ 55 | cin >> x >> y >> z; 56 | if(x==1){ 57 | SQRT_update(y,z); 58 | }else cout << SQRT_query(y,z) < tree; 26 | 27 | void build(int x, int l, int r) { 28 | if (l == r) { 29 | return; 30 | } 31 | int y = (l + r) >> 1; 32 | int z = x + ((y - l + 1) << 1); 33 | build(x + 1, l, y); 34 | build(z, y + 1, r); 35 | pull(x, z); 36 | } 37 | 38 | template 39 | void build(int x, int l, int r, const vector &v) { 40 | if (l == r) { 41 | tree[x].apply(l, r, v[l]); 42 | return; 43 | } 44 | int y = (l + r) >> 1; 45 | int z = x + ((y - l + 1) << 1); 46 | build(x + 1, l, y, v); 47 | build(z, y + 1, r, v); 48 | pull(x, z); 49 | } 50 | 51 | node get(int x, int l, int r, int ll, int rr) { 52 | if (ll <= l && r <= rr) { 53 | return tree[x]; 54 | } 55 | int y = (l + r) >> 1; 56 | int z = x + ((y - l + 1) << 1); 57 | push(x, l, r); 58 | node res{}; 59 | if (rr <= y) { 60 | res = get(x + 1, l, y, ll, rr); 61 | } else { 62 | if (ll > y) { 63 | res = get(z, y + 1, r, ll, rr); 64 | } else { 65 | res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr)); 66 | } 67 | } 68 | pull(x, z); 69 | return res; 70 | } 71 | 72 | template 73 | void modify(int x, int l, int r, int ll, int rr, const M&... v) { 74 | if (ll <= l && r <= rr) { 75 | tree[x].apply(l, r, v...); 76 | return; 77 | } 78 | int y = (l + r) >> 1; 79 | int z = x + ((y - l + 1) << 1); 80 | push(x, l, r); 81 | if (ll <= y) { 82 | modify(x + 1, l, y, ll, rr, v...); 83 | } 84 | if (rr > y) { 85 | modify(z, y + 1, r, ll, rr, v...); 86 | } 87 | pull(x, z); 88 | } 89 | 90 | int find_first_knowingly(int x, int l, int r, const function &f) { 91 | if (l == r) { 92 | return l; 93 | } 94 | push(x, l, r); 95 | int y = (l + r) >> 1; 96 | int z = x + ((y - l + 1) << 1); 97 | int res; 98 | if (f(tree[x + 1])) { 99 | res = find_first_knowingly(x + 1, l, y, f); 100 | } else { 101 | res = find_first_knowingly(z, y + 1, r, f); 102 | } 103 | pull(x, z); 104 | return res; 105 | } 106 | 107 | int find_first(int x, int l, int r, int ll, int rr, const function &f) { 108 | if (ll <= l && r <= rr) { 109 | if (!f(tree[x])) { 110 | return -1; 111 | } 112 | return find_first_knowingly(x, l, r, f); 113 | } 114 | push(x, l, r); 115 | int y = (l + r) >> 1; 116 | int z = x + ((y - l + 1) << 1); 117 | int res = -1; 118 | if (ll <= y) { 119 | res = find_first(x + 1, l, y, ll, rr, f); 120 | } 121 | if (rr > y && res == -1) { 122 | res = find_first(z, y + 1, r, ll, rr, f); 123 | } 124 | pull(x, z); 125 | return res; 126 | } 127 | 128 | int find_last_knowingly(int x, int l, int r, const function &f) { 129 | if (l == r) { 130 | return l; 131 | } 132 | push(x, l, r); 133 | int y = (l + r) >> 1; 134 | int z = x + ((y - l + 1) << 1); 135 | int res; 136 | if (f(tree[z])) { 137 | res = find_last_knowingly(z, y + 1, r, f); 138 | } else { 139 | res = find_last_knowingly(x + 1, l, y, f); 140 | } 141 | pull(x, z); 142 | return res; 143 | } 144 | 145 | int find_last(int x, int l, int r, int ll, int rr, const function &f) { 146 | if (ll <= l && r <= rr) { 147 | if (!f(tree[x])) { 148 | return -1; 149 | } 150 | return find_last_knowingly(x, l, r, f); 151 | } 152 | push(x, l, r); 153 | int y = (l + r) >> 1; 154 | int z = x + ((y - l + 1) << 1); 155 | int res = -1; 156 | if (rr > y) { 157 | res = find_last(z, y + 1, r, ll, rr, f); 158 | } 159 | if (ll <= y && res == -1) { 160 | res = find_last(x + 1, l, y, ll, rr, f); 161 | } 162 | pull(x, z); 163 | return res; 164 | } 165 | 166 | segtree(int _n) : n(_n) { 167 | assert(n > 0); 168 | tree.resize(2 * n - 1); 169 | build(0, 0, n - 1); 170 | } 171 | 172 | template 173 | segtree(const vector &v) { 174 | n = v.size(); 175 | assert(n > 0); 176 | tree.resize(2 * n - 1); 177 | build(0, 0, n - 1, v); 178 | } 179 | 180 | node get(int ll, int rr) { 181 | assert(0 <= ll && ll <= rr && rr <= n - 1); 182 | return get(0, 0, n - 1, ll, rr); 183 | } 184 | 185 | node get(int p) { 186 | assert(0 <= p && p <= n - 1); 187 | return get(0, 0, n - 1, p, p); 188 | } 189 | 190 | template 191 | void modify(int ll, int rr, const M&... v) { 192 | assert(0 <= ll && ll <= rr && rr <= n - 1); 193 | modify(0, 0, n - 1, ll, rr, v...); 194 | } 195 | 196 | // find_first and find_last call all FALSE elements 197 | // to the left (right) of the sought position exactly once 198 | 199 | int find_first(int ll, int rr, const function &f) { 200 | assert(0 <= ll && ll <= rr && rr <= n - 1); 201 | return find_first(0, 0, n - 1, ll, rr, f); 202 | } 203 | 204 | int find_last(int ll, int rr, const function &f) { 205 | assert(0 <= ll && ll <= rr && rr <= n - 1); 206 | return find_last(0, 0, n - 1, ll, rr, f); 207 | } 208 | }; 209 | 210 | -------------------------------------------------------------------------------- /Seive of erasthones.cpp: -------------------------------------------------------------------------------- 1 | void seive(){ 2 | vector prime; 3 | prime.resize(MAX,true); 4 | 5 | prime[0] = prime[1] = false; 6 | for (int i = 2; i <= MAX; ++i) 7 | { 8 | if(prime[i] && (long long)i*i <= MAX){ 9 | for (int j = i*i; j <= MAX; j+=i) 10 | { 11 | prime[j] = false; 12 | } 13 | } 14 | } 15 | 16 | for (int i = 2; i <= MAX; ++i) 17 | { 18 | if(prime[i])primes.push_back(i); 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /Size_of_subtree.cpp: -------------------------------------------------------------------------------- 1 | 2 | vector G[MAX],child; 3 | int sum=0; 4 | int dfs(int u,int par) 5 | { 6 | child[u] = 1; 7 | for(auto x : G[u]) 8 | { 9 | if(x == par)continue; 10 | dfs(x,u); 11 | child[u] += child[x]; 12 | } 13 | return 0; 14 | } 15 | int32_t main() { 16 | IOS; 17 | int n,m,u,v; 18 | cin >> n >> m; 19 | rep(i,0,m) 20 | { 21 | cin >> u >> v; 22 | G[u].pb(v);G[v].pb(u); 23 | } 24 | child.resize(n+1); 25 | dfs(1,0); 26 | rep(i,1,n+1)cout << child[i] << ' '; 27 | return 0; 28 | } 29 | 30 | //INPUT 31 | // 1 32 | // / \ 33 | // 2 3 34 | // / \ 35 | // 4 5 36 | 37 | //OUTPUT 38 | // 5 3 1 1 1 39 | -------------------------------------------------------------------------------- /Solution.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.*; 3 | import java.text.*; 4 | import java.math.*; 5 | import java.util.regex.*; 6 | 7 | public class Solution { 8 | public static void main(String[] args) 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | //code to read values 12 | Movie objArr[] = new Movie[4]; 13 | for (int i = 0; i < 4; ++i) { 14 | int w,y,z; 15 | String x; 16 | w = sc.nextInt(); 17 | x = sc.next(); 18 | y = sc.nextInt(); 19 | z = sc.nextInt(); 20 | objArr[i] = new Movie(w,x,y,z); 21 | } 22 | String dir; 23 | int rat, bud; 24 | dir = sc.next(); 25 | rat = sc.nextInt(); 26 | bud = sc.nextInt(); 27 | int avg = findAvgBudgetByDirector(objArr, dir); 28 | if (avg != -1) { 29 | System.out.println(avg); 30 | } 31 | Movie res = getMovieByRatingBudget(objArr, rat, bud); 32 | } 33 | 34 | public static int findAvgBudgetByDirector (Movie[] movies, String director) 35 | { 36 | int sum = 0, count = 0; 37 | for (int i = 0; i < 4; ++i) { 38 | if(movies[i].director.equalsIgnoreCase(director)) { 39 | sum += movies[i].budget; 40 | count++; 41 | } 42 | } 43 | if (sum == 0) { 44 | System.out.println("Sorry - The given director has not yet directed any movie"); 45 | return -1; 46 | } 47 | else { 48 | int average = (sum / count); 49 | return average; 50 | } 51 | } 52 | 53 | public static Movie getMovieByRatingBudget(Movie[] movies, int rating, int budget) 54 | { 55 | Movie obj = new Movie(0,"",0,0); 56 | if (budget%rating != 0) { 57 | System.out.println("Sorry - No movie is available with the specified rating and budget requirement"); 58 | return obj; 59 | } 60 | int res = 0; 61 | for (int i = 0; i < 4; ++i) { 62 | if (movies[i].rating == rating && movies[i].budget == budget) 63 | res = movies[i].movieId; 64 | } 65 | System.out.println(res); 66 | return obj; 67 | } 68 | } 69 | 70 | class Movie 71 | { 72 | int movieId, rating, budget; 73 | String director; 74 | 75 | public Movie(int a, String b, int c, int d) { 76 | movieId = a; 77 | director = b; 78 | rating = c; 79 | budget = d; 80 | 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /Strongly_connected_component.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela - Soham Chakrabarti */ 2 | 3 | /* problem Link - https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/practice-problems/algorithm/magical-islands/description/ */ 4 | /* Solution Link - GIVEN BELOW */ 5 | 6 | /* problem Link - https://www.hackerearth.com/practice/algorithms/graphs/strongly-connected-components/practice-problems/algorithm/a-walk-to-remember-qualifier2/ */ 7 | /* Solution - https://www.hackerearth.com/submission/39856526/ */ 8 | 9 | 10 | #include 11 | 12 | using namespace std; 13 | 14 | #define IO ios_base::sync_with_stdio(false);cin.tie(NULL) 15 | #define all(v) v.begin(),v.end() 16 | #define pb push_back 17 | #define FR(i,j,k) for (ll i=j;i=k;--i) 19 | #define ff first 20 | #define ss second 21 | 22 | typedef long long int ll; 23 | typedef unsigned long long ull; 24 | typedef long double ld; 25 | typedef pair pii; 26 | 27 | const int N = 1e5+5; 28 | const int M = 101; 29 | const int inf = INT_MAX; 30 | const int MOD = 1e7+9; 31 | 32 | int n,m,p,q; 33 | vector G1[N],G2[N]; 34 | vector node,lis; 35 | bool v[N]; 36 | 37 | void dfs1(int u) 38 | { 39 | v[u]=1; 40 | for(auto &x:G1[u]){ 41 | if(!v[x])dfs1(x); 42 | } 43 | node.pb(u); 44 | } 45 | void dfs2(int u) 46 | { 47 | v[u]=1; 48 | lis.pb(u); 49 | for(auto &x:G2[u]){ 50 | if(!v[x])dfs2(x); 51 | } 52 | 53 | } 54 | 55 | int main() { 56 | IO; 57 | cin>>n>>m; 58 | FR(i,0,m) 59 | { 60 | cin>>p>>q; 61 | --p,--q; 62 | G1[p].pb(q); 63 | G2[q].pb(p); 64 | } 65 | 66 | memset(v,0,sizeof(v)); 67 | FR(i,0,n){ 68 | if(!v[i])dfs1(i); 69 | } 70 | int out=0; 71 | memset(v,0,sizeof(v)); 72 | reverse(all(node)); 73 | for(int x:node){ 74 | lis.clear(); 75 | if(!v[x]){ 76 | dfs2(x); 77 | out=max(out,(int)lis.size()); 78 | } 79 | } 80 | cout< G[MAX],child; 2 | int sum=0; 3 | int dfs(int u,int par,int sum=0) 4 | { 5 | child[u] = u; 6 | for(auto x : G[u]) 7 | { 8 | if(x == par)continue; 9 | dfs(x,u,0); 10 | sum += child[x]; 11 | } 12 | child[u] += sum; 13 | return 0; 14 | } 15 | int32_t main() { 16 | IOS; 17 | int n,m,u,v; 18 | cin >> n >> m; 19 | rep(i,0,m) 20 | { 21 | cin >> u >> v; 22 | G[u].pb(v);G[v].pb(u); 23 | } 24 | child.resize(n+1); 25 | dfs(1,0); 26 | rep(i,1,n+1)cout << child[i] << ' '; 27 | return 0; 28 | } 29 | 30 | 31 | // 1 32 | // / \ 33 | // 2 3 34 | // / \ 35 | // 4 5 36 | 37 | // 1 2 3 4 5 38 | // 15 11 3 4 5 39 | -------------------------------------------------------------------------------- /TerribleMathematics.cpp: -------------------------------------------------------------------------------- 1 | #pragma Gpref optimize("O3") 2 | //#pragma Gpref optimize("Ofast") 3 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 4 | //#pragma Gpref optimize("unroll-loops") 5 | #include 6 | #define int long long 7 | #define ld long double 8 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 9 | #define TC int t; cin >> t; while(t--) 10 | #define rep(i,j,k) for(int i=j; i=k; --i) 12 | #define pb push_back 13 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 14 | #define F first 15 | #define S second 16 | using namespace std; 17 | 18 | const int MOD = (int)1e9+7; 19 | const int MX = 105; 20 | const int N=105; 21 | typedef pair prs; 22 | int mat[MX][MX]; 23 | 24 | int cccccc=INT_MIN; 25 | 26 | int bitstodec(string n,int m) 27 | { 28 | string num = n; 29 | int dec_value = 0; 30 | int base = 1; 31 | int len = num.length(); 32 | for (int i = len - 1; i >= 0; i--) { 33 | if (num[i] == '1') 34 | dec_value += base; 35 | base = base * 2; 36 | base%=m; dec_value%=m; 37 | } 38 | 39 | return dec_value%m; 40 | } 41 | int32_t main(){ 42 | //freopen("ic1.txt", "r", stdin); 43 | //freopen("oc1.txt", "w", stdout); 44 | IOS; 45 | TC{ 46 | int n,k,len,c=0,y,z; 47 | cin >> n >> k; 48 | string s;cin >> s; 49 | len=s.length(); 50 | rep(i,0,len){ 51 | if(s[i] == '_')c++; 52 | } 53 | int x=pow(2,c);int gg=x; 54 | char bits[gg][c]; 55 | rep(i,0,c){ 56 | x/=2;y=x,z=x; 57 | rep(j,0,gg){ 58 | if(y!=0 && z!=0){bits[j][i]='0';y--;} 59 | else {bits[j][i]='1';z--;} 60 | if(y==0&&z==0){y=x;z=x;} 61 | } 62 | } 63 | int cnt=0; 64 | rep(i,0,gg){ 65 | string ss="";int hh=0; 66 | rep(j,0,n){ 67 | if(s[j]=='_')ss+=(bits[i][hh++]);else ss+=s[j]; 68 | } 69 | if(bitstodec(ss,k)%k==0)cnt++; 70 | }cout< 5 | 6 | using namespace std; 7 | 8 | int main() { 9 | // freopen("i3.txt", "w", stdout); 10 | 11 | srand(time(NULL)); 12 | 13 | std::random_device rd; 14 | std::mt19937 eng(rd()); 15 | std::uniform_int_distribution < > distr(1, 100); 16 | //static const char generator[] = "abcdefghijklmnopqrstuvwxyz"; 17 | //static const char leet[] = "12"; 18 | 19 | //TEST CASES SET BY USER 20 | int tc = 1; 21 | 22 | cout << tc << '\n'; 23 | 24 | while (tc--) { 25 | 26 | // MAXIMUM NODES OF TREE 27 | const int maxN = 10; 28 | const int N = rand() % maxN + 1; 29 | 30 | //PRINT NODES OF TREE 31 | cout << N << endl; 32 | 33 | //MIN AND MAX VALUE OF NODE OF TREE 34 | int x = 1, y = 10; 35 | 36 | //VALUES OF NODES (IF TO BE PROVIDED) 37 | for (int i = 0; i < N; ++i) { 38 | cout << x + rand() % ((y + 1) - (x)) << ' '; 39 | } 40 | cout << endl; 41 | 42 | //BASE TREE TEST DATA STARTS 43 | vector < int > nodeIds; 44 | for (int i = 1; i <= N; i++) { 45 | nodeIds.push_back(i); 46 | } 47 | random_shuffle(nodeIds.begin(), nodeIds.end()); 48 | 49 | vector < int > usedNodeIds = { 50 | nodeIds.back() 51 | }; 52 | nodeIds.pop_back(); 53 | 54 | for (int i = 0; i < N - 1; i++) { 55 | const int newNodeId = nodeIds.back(); 56 | const int oldNodeId = usedNodeIds[rand() % usedNodeIds.size()]; 57 | 58 | cout << newNodeId << " " << oldNodeId << endl; 59 | 60 | usedNodeIds.push_back(newNodeId); 61 | nodeIds.pop_back(); 62 | } 63 | //BASE TREE TEST DATA ENDS 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Topological Sort.cpp: -------------------------------------------------------------------------------- 1 | 2 | int n; // number of vertices 3 | vector> adj; // adjacency list of graph 4 | vector visited; 5 | vector ans; 6 | 7 | void dfs(int v) { 8 | visited[v] = true; 9 | for (int u : adj[v]) { 10 | if (!visited[u]) 11 | dfs(u); 12 | } 13 | ans.push_back(v); 14 | } 15 | 16 | 17 | 18 | void topological_sort() { 19 | visited.assign(n, false); 20 | ans.clear(); 21 | for (int i = 0; i < n; ++i) { 22 | if (!visited[i]) 23 | dfs(i); 24 | } 25 | reverse(ans.begin(), ans.end()); 26 | } 27 | 28 | 29 | int32_t main() 30 | { 31 | //inputs and pass to topological_sort() 32 | } 33 | 34 | /* 35 | ----------------------------------------------------------------------------------------------------------------------- 36 | Problem : https://www.codechef.com/COOK105A/problems/DINCPATH 37 | Solution can be found beside 38 | */ 39 | -------------------------------------------------------------------------------- /Treap.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | */ 3 | #include 4 | using namespace std; 5 | 6 | #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); 7 | #define endl "\n" 8 | 9 | mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); 10 | 11 | int getRand(int l, int r) 12 | { 13 | uniform_int_distribution uid(l, r); 14 | return uid(rng); 15 | } 16 | 17 | struct Treap 18 | { 19 | struct data 20 | { 21 | int priority, val, cnt; 22 | data *l, *r; 23 | 24 | data() 25 | { 26 | val = 0, cnt = 0, l = NULL, r = NULL; 27 | } 28 | 29 | data (int _val) 30 | { 31 | val = _val, cnt = 1; 32 | l = NULL, r = NULL; 33 | priority = getRand(1, 2e9); 34 | } 35 | }; 36 | 37 | typedef struct data* node; 38 | 39 | node head; 40 | 41 | Treap(): head(0) {} 42 | 43 | int cnt(node cur) 44 | { 45 | return cur ? cur->cnt : 0; 46 | } 47 | 48 | void updateCnt(node cur) 49 | { 50 | if(cur) 51 | cur->cnt = cnt(cur->l) + cnt(cur->r) + 1; 52 | } 53 | 54 | void push(node cur) //Lazy Propagation 55 | { 56 | ; 57 | } 58 | 59 | void combine(node &cur, node l, node r) 60 | { 61 | if(!l) 62 | { 63 | cur = r; 64 | return; 65 | } 66 | if(!r) 67 | { 68 | cur = l; 69 | return; 70 | } 71 | //Merge Operations like in segment tree 72 | } 73 | 74 | void reset(node &cur) //To reset other fields of cur except value and cnt 75 | { 76 | if(!cur) 77 | return; 78 | } 79 | 80 | void operation(node &cur) 81 | { 82 | if(!cur) 83 | return; 84 | reset(cur); 85 | combine(cur, cur->l, cur); 86 | combine(cur, cur, cur->r); 87 | } 88 | 89 | void splitPos(node cur, node &l, node &r, int k, int add = 0) 90 | { 91 | if(!cur) 92 | return void(l = r = 0); 93 | push(cur); 94 | int idx = add + cnt(cur->l); 95 | if(idx <= k) 96 | splitPos(cur->r, cur->r, r, k, idx + 1), l = cur; 97 | else 98 | splitPos(cur->l, l, cur->l, k, add), r = cur; 99 | updateCnt(cur); 100 | operation(cur); 101 | } 102 | 103 | void split(node cur, node &l, node &r, int k) 104 | { 105 | if(!cur) 106 | return void(l = r = 0); 107 | push(cur); 108 | int idx = cur -> val; 109 | if(idx <= k) 110 | split(cur->r, cur->r, r, k), l = cur; 111 | else 112 | split(cur->l, l, cur->l, k), r = cur; 113 | updateCnt(cur); 114 | operation(cur); 115 | } 116 | 117 | void merge(node &cur, node l, node r) 118 | { 119 | push(l); 120 | push(r); 121 | if(!l || !r) 122 | cur = l ? l : r; 123 | else if(l->priority > r->priority) 124 | merge(l->r, l->r, r), cur = l; 125 | else 126 | merge(r->l, l, r->l), cur = r; 127 | updateCnt(cur); 128 | operation(cur); 129 | } 130 | 131 | void insert(int val) 132 | { 133 | if(!head) 134 | { 135 | head = new data(val); 136 | return; 137 | } 138 | node l, r, mid, mid2, rr; 139 | mid = new data(val); 140 | split(head, l, r, val - 1); 141 | merge(l, l, mid); 142 | split(r, mid2, rr, val); 143 | merge(head, l, rr); 144 | } 145 | 146 | void erase(int val) 147 | { 148 | node l, r, mid; 149 | split(head, l, r, val - 1); 150 | split(r, mid, r, val); 151 | merge(head, l, r); 152 | } 153 | 154 | void inorder(node cur) 155 | { 156 | if(!cur) 157 | return; 158 | push(cur); 159 | inorder(cur->l); 160 | cout<val<<" "; 161 | inorder(cur->r); 162 | } 163 | 164 | void inorder() 165 | { 166 | inorder(head); 167 | cout<l), clear(cur->r); 175 | delete cur; 176 | } 177 | 178 | void clear() 179 | { 180 | clear(head); 181 | } 182 | 183 | int find_by_order(int x) //1 indexed 184 | { 185 | if(!x) 186 | return -1; 187 | x--; 188 | node l, r, mid; 189 | splitPos(head, l, r, x - 1); 190 | splitPos(r, mid, r, 0); 191 | int ans = -1; 192 | if(cnt(mid) == 1) 193 | ans = mid->val; 194 | merge(r, mid, r); 195 | merge(head, l, r); 196 | return ans; 197 | } 198 | 199 | int order_of_key(int val) //1 indexed 200 | { 201 | node l, r, mid; 202 | split(head, l, r, val - 1); 203 | split(r, mid, r, val); 204 | int ans = -1; 205 | if(cnt(mid)== 1) 206 | ans = 1+cnt(l); 207 | merge(r, mid, r); 208 | merge(head, l, r); 209 | return ans; 210 | } 211 | }; 212 | 213 | int32_t main() 214 | { 215 | IOS; 216 | Treap t; 217 | int n,rem=0; 218 | cin >> n; 219 | set st; 220 | while(n--) 221 | { 222 | int num; char query; 223 | cin >> query >> num; 224 | if(query == 'I') 225 | { 226 | //insert query 227 | } 228 | if(query == 'D') 229 | { 230 | //delete query 231 | } 232 | if(query == 'K') 233 | { 234 | //kth samllest elem 235 | } 236 | if(query == 'C') 237 | { 238 | no. of elems before c in treap 239 | } 240 | } 241 | 242 | return 0; 243 | } 244 | -------------------------------------------------------------------------------- /Tree to Prufer code.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Tree to Prufer Code 3 | Complexity: O(VlogV) 4 | */ 5 | 6 | vector treeToPrufercode (int nodes, vector> &edges) { 7 | unordered_set neighbors[nodes+1]; // For each node, who is it's neighbor? 8 | 9 | for( int i = 0; i < edges.size(); i++ ) { 10 | pair edge = edges[i]; 11 | int u = edges[i].first; int v = edges[i].second; 12 | neighbors[u].insert(v); 13 | neighbors[v].insert(u); 14 | } 15 | 16 | priority_queue leaves; 17 | for ( int i = 0; i <= nodes; i++ ) { 18 | if (neighbors[i].size() == 1 ) { 19 | leaves.push(-i); // Negating since we need min heap 20 | } 21 | } 22 | vector pruferCode; 23 | int need = nodes - 2; 24 | while(need--) { 25 | int leaf = -leaves.top(); leaves.pop(); 26 | int neighborOfLeaf = *(neighbors[leaf].begin()); 27 | pruferCode.push_back(neighborOfLeaf); 28 | // Remove the leaf 29 | neighbors[neighborOfLeaf].erase(leaf); 30 | // The neighbor can become a new leaf 31 | if(neighbors[neighborOfLeaf].size() == 1) { 32 | leaves.push(-neighborOfLeaf); 33 | } 34 | } 35 | return pruferCode; 36 | } 37 | -------------------------------------------------------------------------------- /Trie.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela - Soham Chakrabarti */ 2 | 3 | 4 | /* void insert(string) -> insert a lowercase string into the trie*/ 5 | /* bool search(string) -> search a string is present/not present in the trie*/ 6 | /* bool prefix(string) -> search a prefix of a string is present/not present in the trie*/ 7 | /* bool searchAdvanced(string) -> search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.*/ 8 | 9 | 10 | /* 11 | Below Code is entire in C++ 12 | Used a Class named Trie, with data members (isEnd), Trie* t[26] 13 | and member functions mentioned above with their params(...) and work. 14 | 15 | Explaintain of a trie with helpful Snippets: https://leetcode.com/problems/implement-trie-prefix-tree/solution/ 16 | You can solve the problem as well. ;) 17 | */ 18 | 19 | #include 20 | 21 | using namespace std; 22 | 23 | #define io ios_base::sync_with_stdio(false);cin.tie(NULL) 24 | #define all(v) v.begin(),v.end() 25 | #define pb push_back 26 | #define ins insert 27 | #define rep(i,j,k) for(ll i=j;i=k;--i) 29 | #define scan(a,n) rep(i,0,n)cin>>a[i] 30 | #define input freopen("input.txt","r",stdin) 31 | #define output freopen("output.txt","w",stdout) 32 | #define error freopen("error.txt","w",stderr) 33 | #define ff first 34 | #define ss second 35 | 36 | typedef long long int ll; 37 | typedef unsigned long long ull; 38 | typedef long double ld; 39 | 40 | const int N = 2e5+1; 41 | const int MAX = 1e6+1; 42 | const int ALPHA = 26; 43 | const int inf = INT_MAX; 44 | const int mod = 1e9+7; 45 | 46 | ll powm(ll a,ll b) {ll res=1LL;while(b) {if(b&1)res=(res*a)%mod;a=(a*a)%mod;b>>=1;}return res;} 47 | ll modmult(ll a,ll b) {ll r=0;a%=mod;while(b){if(b&1)r=(r+a)%mod;a=(a<<1)%mod;b>>=1;}return r;} 48 | ll modexpo(ll a,ll b) {ll r=1;a%=mod;while(b){if(b&1)r=(r*a)%mod;a=(a*a)%mod;b>>=1;}return r;} 49 | 50 | 51 | struct Trie{ 52 | bool isEnd; 53 | Trie* t[ALPHA]; 54 | public: 55 | Trie() { 56 | isEnd = false; 57 | rep(i,0,ALPHA)t[i]=NULL; 58 | } 59 | 60 | // Insert a Word into the Trie 61 | void insertWord(string word) { 62 | Trie* root = this; 63 | for (int i = 0; i < word.size(); ++i) { 64 | int idx = word[i] - 'a'; 65 | if (root->t[idx] == NULL) 66 | root->t[idx] = new Trie(); 67 | root = root->t[idx]; 68 | } 69 | root->isEnd = true; 70 | } 71 | 72 | // Search a Word in the trie 73 | bool searchWord(string word) { 74 | Trie* root = this; 75 | for (int i = 0; i < word.size(); ++i) { 76 | int idx = word[i] - 'a'; 77 | if (root->t[idx] == NULL) return false; 78 | root = root->t[idx]; 79 | } 80 | 81 | return root->isEnd; 82 | } 83 | 84 | // Check if any word has the 'prefix' 85 | bool hasPrefix(string prefix) { 86 | Trie* root = this; 87 | bool present = true; 88 | if(root == NULL) return false; 89 | for (int i = 0; i < prefix.size(); ++i) { 90 | int idx = prefix[i] - 'a'; 91 | if(root->t[idx] == NULL) { 92 | present = false; 93 | break; 94 | } 95 | root = root->t[idx]; 96 | 97 | } 98 | return present; 99 | } 100 | 101 | bool dfsTrie(Trie* root, string word, int index, int len) { 102 | if(index == len) 103 | return root->isEnd; // end of string search 104 | 105 | if(word[index] != '.') { 106 | if(root->t[word[index]-'a']) 107 | return dfsTrie(root->t[word[index]-'a'], word, index+1, len); 108 | else 109 | return false; 110 | } 111 | 112 | for (int i = 0; i < 26; ++i) { 113 | if(root->t[i]) 114 | if(dfsTrie(root->t[i], word, index+1, len)) return true; 115 | } 116 | return false; 117 | } 118 | 119 | bool searchAdvanced(string word) { 120 | Trie* root = this; 121 | int sz = word.size(); 122 | return dfsTrie(root, word, 0, sz); 123 | } 124 | }; 125 | 126 | int main() { 127 | io; 128 | Trie* obj = new Trie(); 129 | 130 | obj->insertWord("apple"); 131 | obj->insertWord("bunny"); 132 | obj->insertWord("buck"); 133 | 134 | cout << "Search the Word -> 'app' : "<< (obj->searchWord("app")?"Present":"Not present") << endl; 135 | cout << "Search the Word -> 'apple' : "<< (obj->searchWord("apple")?"Present":"Not present") << endl; 136 | cout << "Search the Word -> 'appleo' : "<< (obj->searchWord("appleo")?"Present":"Not present") << endl; 137 | 138 | cout << "Prefix -> 'app' : "<< (obj->hasPrefix("app")?"Present":"Not present") << endl; 139 | cout << "Prefix -> 'apple' : "<< (obj->hasPrefix("apple")?"Present":"Not present") << endl; 140 | cout << "Prefix -> 'appleo' : "<< (obj->hasPrefix("appleo")?"Present":"Not present") << endl; 141 | 142 | cout << "searchAdvanced -> '.uck' : "<< (obj->searchAdvanced(".uck")?"Present":"Not present") << endl; 143 | cout << "searchAdvanced -> 'a.p.e' : "<< (obj->searchAdvanced("a.p.e")?"Present":"Not present") << endl; 144 | cout << "searchAdvanced -> 'bunn.' : "<< (obj->searchAdvanced("bunn.")?"Present":"Not present") << endl; 145 | 146 | 147 | return 0; 148 | } 149 | 150 | /* 151 | 152 | Output: 153 | Search the Word -> 'app' : Not present 154 | Search the Word -> 'apple' : Present 155 | Search the Word -> 'appleo' : Not present 156 | Prefix -> 'app' : Present 157 | Prefix -> 'apple' : Present 158 | Prefix -> 'appleo' : Not present 159 | searchAdvanced -> '.uck' : Present 160 | searchAdvanced -> 'a.p.e' : Present 161 | searchAdvanced -> 'bunn.' : Present 162 | 163 | */ 164 | -------------------------------------------------------------------------------- /Unique Characters in String.cpp: -------------------------------------------------------------------------------- 1 | 2 | //Problem Link and Solution : https://codeforces.com/contest/1234/submission/61672539 3 | //Codeforces D 4 | 5 | #include 6 | 7 | #define ll long long 8 | #define ld long double 9 | 10 | #define FR(i,j,k) for(int i=j;i> T(4*MAX, vector(26)); 20 | void build(int node,int s,int e){ 21 | if(s==e){ 22 | T[node][arr[s]-'a']++; 23 | return; 24 | } 25 | int m=(s+e)>>1; 26 | build(node<<1,s,m); 27 | build(node<<1|1,m+1,e); 28 | FR(i,0,26)T[node][i]=T[node<<1][i] + T[node<<1|1][i]; 29 | } 30 | vector query(int node,int s,int e,int l,int r){ 31 | vector a(26); 32 | if(er) return a; //no overlap 33 | if(l<=s && r>=e) return T[node]; 34 | int mid = (s+e)>>1; 35 | vector a1 = query(node<<1,s,mid,l,r); 36 | vector a2 = query(node<<1|1,mid+1,e,l,r); 37 | FR(i,0,26)a[i] = a1[i] + a2[i]; 38 | return a; 39 | 40 | } 41 | void upd(int node,int s,int e,int p,char new_val){ 42 | if(s == e){ 43 | T[node][arr[p] - 'a'] --; 44 | T[node][new_val - 'a'] ++; 45 | arr[p]=new_val; 46 | } 47 | else 48 | { 49 | int mid = (s+e)>>1; 50 | if(p <= mid) 51 | { 52 | upd(node<<1, s, mid, p, new_val); 53 | } 54 | else 55 | { 56 | upd(node<<1|1, mid+1, e, p, new_val); 57 | } 58 | FR(i,0,26){T[node][i]=T[node<<1][i] + T[node<<1|1][i];} 59 | } 60 | } 61 | int32_t main() { 62 | IOS; 63 | cin>>arr; 64 | int ln=arr.length(); 65 | build(1,0,ln-1); 66 | int q; 67 | cin>>q; 68 | while(q--){ 69 | int x; 70 | cin>>x; 71 | if(x==1){ 72 | int p;char ch; 73 | cin>>p>>ch; 74 | upd(1,0,ln-1,p-1,ch); 75 | }else{ 76 | int l,r; 77 | cin>>l>>r; 78 | vector res = query(1,0,ln-1,l-1,r-1); 79 | int an=0; 80 | FR(i,0,26){if(res[i]>0)an++;} 81 | cout< 8 | #define pb push_back 9 | int lo, hi; 10 | wavelet_tree *l, *r; 11 | vi b; 12 | 13 | //nos are in range [x,y] 14 | //array indices are [from, to) 15 | wavelet_tree(int *from, int *to, int x, int y){ 16 | lo = x, hi = y; 17 | if(lo == hi or from >= to) return; 18 | int mid = (lo+hi)/2; 19 | auto f = [mid](int x){ 20 | return x <= mid; 21 | }; 22 | b.reserve(to-from+1); 23 | b.pb(0); 24 | for(auto it = from; it != to; it++) 25 | b.pb(b.back() + f(*it)); 26 | //see how lambda function is used here 27 | auto pivot = stable_partition(from, to, f); 28 | l = new wavelet_tree(from, pivot, lo, mid); 29 | r = new wavelet_tree(pivot, to, mid+1, hi); 30 | } 31 | 32 | //kth smallest element in [l, r] 33 | int kth(int l, int r, int k){ 34 | if(l > r) return 0; 35 | if(lo == hi) return lo; 36 | int inLeft = b[r] - b[l-1]; 37 | int lb = b[l-1]; //amt of nos in first (l-1) nos that go in left 38 | int rb = b[r]; //amt of nos in first (r) nos that go in left 39 | if(k <= inLeft) return this->l->kth(lb+1, rb , k); 40 | return this->r->kth(l-lb, r-rb, k-inLeft); 41 | } 42 | 43 | //count of nos in [l, r] Less than or equal to k 44 | int LTE(int l, int r, int k) { 45 | if(l > r or k < lo) return 0; 46 | if(hi <= k) return r - l + 1; 47 | int lb = b[l-1], rb = b[r]; 48 | return this->l->LTE(lb+1, rb, k) + this->r->LTE(l-lb, r-rb, k); 49 | } 50 | 51 | //count of nos in [l, r] equal to k 52 | int count(int l, int r, int k) { 53 | if(l > r or k < lo or k > hi) return 0; 54 | if(lo == hi) return r - l + 1; 55 | int lb = b[l-1], rb = b[r], mid = (lo+hi)/2; 56 | if(k <= mid) return this->l->count(lb+1, rb, k); 57 | return this->r->count(l-lb, r-rb, k); 58 | } 59 | ~wavelet_tree(){ 60 | delete l; 61 | delete r; 62 | } 63 | }; 64 | int main() 65 | { 66 | ios_base::sync_with_stdio(false); 67 | cin.tie(NULL); 68 | srand(time(NULL)); 69 | int i,n,k,j,q,l,r; 70 | cin >> n; 71 | fo(i, n) cin >> a[i+1]; 72 | wavelet_tree T(a+1, a+n+1, 1, MAX); 73 | cin >> q; 74 | while(q--){ 75 | int x; 76 | cin >> x; 77 | cin >> l >> r >> k; 78 | if(x == 0){ 79 | //kth smallest 80 | cout << "Kth smallest: "; 81 | cout << T.kth(l, r, k) << endl; 82 | } 83 | if(x == 1){ 84 | //less than or equal to K 85 | cout << "LTE: "; 86 | cout << T.LTE(l, r, k) << endl; 87 | } 88 | if(x == 2){ 89 | //count occurence of K in [l, r] 90 | cout << "Occurence of K: "; 91 | cout << T.count(l, r, k) << endl; 92 | } 93 | } 94 | return 0; 95 | } 96 | //===================================================================================== 97 | 98 | #include 99 | 100 | using namespace std; 101 | 102 | typedef vector ::iterator iter; 103 | 104 | class WaveletTree { 105 | 106 | public: 107 | WaveletTree *left = 0, *right = 0; 108 | int lo, hi, mid; 109 | vector ct; 110 | 111 | WaveletTree(iter b, iter e, int lo_, int hi_) { 112 | lo = lo_, hi = hi_; 113 | mid = (lo + hi) >> 1; 114 | if (b >= e) return; 115 | ct.reserve(e - b + 1); 116 | ct.emplace_back(0); 117 | for (auto it = b; it != e; it++) { 118 | ct.emplace_back(ct.back() + ((*it) <= mid)); 119 | } 120 | iter pivot = stable_partition(b, e, [=](const int& i) { return i <= mid; } ); 121 | if (lo == hi) return; 122 | left = new WaveletTree(b, pivot, lo, mid); 123 | right = new WaveletTree(pivot, e, mid + 1, hi); 124 | }; 125 | 126 | ~WaveletTree() { 127 | delete left; 128 | delete right; 129 | } 130 | 131 | int ocurrences(int a, int b, int k) { 132 | return ocurrences(b, k) - ocurrences(a - 1, k); 133 | } 134 | 135 | int kth(int a, int b, int k) { 136 | if (lo == hi) return lo; 137 | int inLeft = ct[b] - ct[a - 1]; 138 | if (k <= inLeft) return left -> kth(ct[a - 1] + 1, ct[b], k); 139 | return right -> kth(a - ct[a - 1], b - ct[b], k - inLeft); 140 | } 141 | 142 | int range(int x, int y, int a, int b) { 143 | return range(x, y, b) - range(x, y, a - 1); 144 | } 145 | 146 | private: 147 | int range(int x, int y, int b) { 148 | if (hi < x or y < lo) return 0; 149 | if (x <= lo and hi <= y) return b; 150 | int sum = 0; 151 | if (left) sum = left -> range(x, y, ct[b]); 152 | if (right) sum += right -> range(x, y, b - ct[b]); 153 | return sum; 154 | } 155 | 156 | int ocurrences(int b, int k) { 157 | if (k < lo or k > hi) return 0; 158 | if (lo == hi) return b; 159 | if (k <= mid) return left -> ocurrences(ct[b], k); 160 | return right -> ocurrences(b - ct[b], k); 161 | } 162 | }; 163 | -------------------------------------------------------------------------------- /Z Algorithm.cpp: -------------------------------------------------------------------------------- 1 | vector z_function(string &s) 2 | { 3 | int n=s.size(); 4 | vector z(n); 5 | for(int i=1,l=0,r=0;ir) 12 | l=i, r=i+z[i]-1; 13 | } 14 | return z; 15 | } 16 | 17 | /* 18 | 19 | 20 | text = a b c b c a c d e x y 21 | pattern = abc 22 | 23 | new string = a b c ($) a b c b c a c d e x y 24 | 25 | now perform z algorithm to fetch the number of times the pattern exixts in the array! 26 | */ 27 | -------------------------------------------------------------------------------- /Zeller's congruence.py: -------------------------------------------------------------------------------- 1 | def zellercongruence(day,month,year): 2 | if(month==1): 3 | year-=1 4 | month=13 5 | if(month==2): 6 | year-=1 7 | month=14 8 | q=day 9 | m=month 10 | k=year%100 11 | j=year//100 12 | h=q+13*(m + 1)//5+k+k//4+j//4+5*j 13 | h%=7 14 | return h 15 | 16 | print(zellercongruence(7,18,2020)) -------------------------------------------------------------------------------- /hackerearth/Coloring trees.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela - Soham Chakrabarti */ 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | #define IO ios_base::sync_with_stdio(false); cin.tie(NULL); 8 | #define endl '\n'; 9 | 10 | typedef long long ll; 11 | typedef long double ld; 12 | 13 | const int N = 2e5 + 5; 14 | const int MOD = 1e9 + 7; 15 | 16 | vector G[N], deg; 17 | vector is; 18 | 19 | int main() { 20 | IO; 21 | int n, k; 22 | cin >> n >> k; 23 | 24 | deg.assign(n + 1, 0); 25 | is.assign(n + 1, false); 26 | for (int i = 1; i < n; ++i) 27 | { 28 | int u, v; 29 | cin >> u >> v; 30 | G[u].push_back(v); 31 | G[v].push_back(u); 32 | ++deg[u]; ++deg[v]; 33 | } 34 | 35 | for (int i = 0; i < k; ++i) 36 | { 37 | int x; cin >> x; 38 | is[x] = true; 39 | } 40 | 41 | queue nodes; 42 | for (int i = 1; i < (n + 1); ++i) 43 | { 44 | if(!is[i] && deg[i] == 1) { //leaf nodes and not terminus 45 | nodes.push(i); 46 | } 47 | } 48 | 49 | int nvis = 0; 50 | while(!nodes.empty()) { 51 | int data = nodes.front(); 52 | // cout << data << endl; 53 | nodes.pop(); 54 | for (auto u : G[data]) { 55 | remove(G[u].begin(), G[u].end(), data); 56 | deg[u]--; 57 | } 58 | for (auto u : G[data]) { 59 | if(!is[u] && deg[u] == 1) { 60 | nodes.push(u); 61 | } 62 | } 63 | nvis++; 64 | } 65 | 66 | cout << (n - nvis); 67 | cout << endl; 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /hackerearth/Help the Avengers.cpp: -------------------------------------------------------------------------------- 1 | /* Problem Link : https://www.hackerearth.com/practice/data-structures/advanced-data-structures/segment-trees/practice-problems/algorithm/help-the-avengers-6/ */ 2 | #include 3 | #define int long long 4 | using namespace std; 5 | 6 | const int N = 1e6 + 5; 7 | 8 | int isp[N], a[N]; 9 | 10 | void pre() { 11 | for(int i = 0; i < N; ++i) isp[i] = 1; 12 | isp[0] = isp[1] = 0; 13 | 14 | for(long long i = 2; i <= N; ++i) { 15 | if(isp[i]) { 16 | for(long long j = i * i; j <= N; j += i) 17 | isp[j] = 0; 18 | } 19 | } 20 | } 21 | 22 | class SegmentTree { 23 | public: 24 | int tree[4 * N]; 25 | void build(int node, int s, int e) { 26 | if(s == e) { // base case 27 | tree[node] = isp[a[s]]; 28 | return; 29 | } 30 | int mid = (s + e) >> 1; 31 | build(node << 1, s, mid); 32 | build(node << 1 | 1, mid + 1, e); 33 | 34 | tree[node] = tree[node << 1] + tree[node << 1 | 1]; 35 | } 36 | 37 | int query(int node, int s, int e, int l, int r) { 38 | if(e < l || s > r) return 0; // no overlap 39 | if(s >= l && e <= r) return tree[node]; // total overlap 40 | int mid = (s + e) >> 1; 41 | return query(node << 1, s, mid, l, r) + query(node << 1 | 1, mid + 1, e, l, r); 42 | } 43 | 44 | void update(int node, int s, int e, int position, int newValue) { 45 | if(s == e) { 46 | tree[node] = isp[newValue]; 47 | } 48 | else { 49 | int mid = (s + e) >> 1; 50 | if(position <= mid) { // go to left of tree 51 | update(node << 1, s, mid, position, newValue); 52 | } 53 | else { 54 | update(node << 1 | 1, mid + 1, e, position, newValue); 55 | } 56 | 57 | tree[node] = tree[node << 1] + tree[node << 1 | 1]; 58 | } 59 | 60 | } 61 | 62 | }; 63 | int32_t main() { 64 | int T; 65 | cin >> T; 66 | pre(); 67 | while(T--) { 68 | int N; 69 | cin >> N; 70 | for(int i = 0; i < N; ++i) cin >> a[i]; 71 | int Q; 72 | cin >> Q; 73 | SegmentTree st; 74 | st.build(1, 0, N - 1); 75 | while(Q--) { 76 | char ch; 77 | cin >> ch; 78 | if(ch == 'A') { 79 | int l, r, ans; 80 | cin >> l >> r; 81 | ans = st.query(1, 0, N - 1, l - 1, r - 1); 82 | cout << ans << endl; 83 | } 84 | else { 85 | int index, val; 86 | cin >> index >> val; 87 | st.update(1, 0, N - 1, index - 1, val); 88 | } 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /hackerearth/Maximum Spanning Tree.cpp: -------------------------------------------------------------------------------- 1 | //CODE ARENA PLAYER 2 | // Maximum Spanning Tree : https://www.hackerearth.com/practice/algorithms/graphs/depth-first-search/practice-problems/algorithm/maximum-spanning-tree/ 3 | 4 | #include 5 | 6 | #define L first 7 | #define R second 8 | #define LL long long 9 | #define PB push_back 10 | #define MP make_pair 11 | #define Size(c) ((int)(c).size()) 12 | #define For(i, c) for(__typeof(c.begin()) i=c.begin();i!=c.end();++i) 13 | 14 | using namespace std; 15 | typedef pair pie; 16 | typedef pair pii; 17 | const int Maxn=1e6+1000; 18 | int Par[Maxn], High[Maxn]; 19 | 20 | int getpar(int fi){ 21 | if(Par[fi]==fi) 22 | return fi; 23 | else 24 | return Par[fi]=getpar(Par[fi]); 25 | } 26 | void Merge(int fi, int se){ 27 | fi=getpar(fi); se=getpar(se); 28 | if(High[fi]>High[se]) 29 | swap(fi, se); 30 | if(High[fi]==High[se]){ 31 | Par[fi]=se; 32 | High[se]++; 33 | }else 34 | Par[fi]=getpar(se); 35 | } 36 | int main(){ 37 | ios_base::sync_with_stdio(false); 38 | int t; 39 | cin >> t; 40 | while(t--){ 41 | int n, m, Min_Weight = 0; 42 | vector Edges; 43 | for(int i=1;i>n>>m; 46 | while(m--){ 47 | int Fi, Se, Th; 48 | cin>>Fi>>Se>>Th; 49 | Edges.PB(MP(Th, MP(Fi, Se))); 50 | } 51 | sort(Edges.begin(), Edges.end()); 52 | for(int i=Size(Edges)-1;i>=0;i--){ 53 | int Fi=Edges[i].R.L, Se=Edges[i].R.R, Th=Edges[i].L; 54 | if(getpar(Fi) != getpar(Se)){ 55 | //Res.PB(MP(Fi, Se)); 56 | Min_Weight+=Th; 57 | Merge(Fi, Se); 58 | } 59 | } 60 | cout<L<<" "<R< 3 | #define int long long 4 | using namespace std; 5 | 6 | 7 | bool isPalindrome(string s) { 8 | int i = 0, j = s.size() - 1; 9 | while(i < j) { 10 | if(s[i] != s[j]) return false; 11 | ++i, --j; 12 | } 13 | return true; 14 | } 15 | int32_t main() { 16 | int T; 17 | cin >> T; 18 | while(T--) { 19 | string a, b; 20 | cin >> a >> b; 21 | int res = 0; 22 | while(a != b) { 23 | if(isPalindrome(a)) ++res; 24 | string hour = a.substr(0,2); 25 | string minute = a.substr(2,2); 26 | if(minute != "59") { 27 | stringstream typecast(minute); 28 | int val; 29 | typecast >> val; 30 | val++; 31 | minute = to_string(val); 32 | if(minute.size() == 1) minute = "0" + minute; 33 | } 34 | else { 35 | minute = "00"; 36 | stringstream typecast(hour); 37 | int val; 38 | typecast >> val; 39 | val++; 40 | hour = to_string(val); 41 | if(hour.size() == 1) hour = "0" + hour; 42 | } 43 | a = ""; 44 | a = hour + minute; 45 | } 46 | if(isPalindrome(a)) ++res; 47 | cout << res << endl; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /hackerearth/Monk and Square Root.cpp: -------------------------------------------------------------------------------- 1 | // CODE ARENA PLAYER 2 | // Problem Link : https://www.hackerearth.com/problem/algorithm/monk-and-square-root/ 3 | 4 | #include 5 | using namespace std; 6 | #define ll long long 7 | int main(){ 8 | ll t; 9 | cin>>t; 10 | while(t--){ 11 | ll n, m; 12 | cin>>n>>m; 13 | ll ans = -1; 14 | for(ll i=0; i 9 | #define int unsigned long long 10 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 11 | #define TC int t; cin >> t; while(t--) 12 | #define FR(i,j,k) for(int i=j; i=k; --i) 14 | #define pb push_back 15 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 16 | #define go continue 17 | #define F first 18 | #define S second 19 | using namespace std; 20 | 21 | 22 | const int MOD = (int)1e9+7; 23 | const int N = 1e8+8; 24 | vector fact; 25 | 26 | int32_t main(){ 27 | IOS; 28 | int n; 29 | cin >> n; 30 | vector v(n); 31 | FR(i,0,n)cin >> v[i]; 32 | int fl=0; 33 | FR(j,2,N){ 34 | fl=0; 35 | int x = v[0]%j; 36 | FR(i,1,n){ 37 | int y = v[i]%j; 38 | if(x!=y){fl=1;break;} 39 | } 40 | if(!fl)cout << j << ' ';} 41 | return 0; 42 | } 43 | 44 | -------------------------------------------------------------------------------- /hackerearth/Rest in peace - 21-1!.py: -------------------------------------------------------------------------------- 1 | //CODE ARENA PLAYER 2 | // Problem Link : https://www.hackerearth.com/practice/algorithms/searching/linear-search/practice-problems/algorithm/rest-in-peace-21-1/ 3 | t = int(input()) 4 | while (t): 5 | t-=1 6 | done = False 7 | n = (input()) 8 | for x in range(len(n)-1): 9 | if n[x] == '2' and n[x+1] == '1': 10 | done = True 11 | break 12 | 13 | if done : 14 | print("The streak is broken!") 15 | continue 16 | 17 | n = int(n) 18 | if(n % 21 == 0): 19 | done = True 20 | 21 | 22 | if done: 23 | print("The streak is broken!") 24 | continue 25 | 26 | 27 | print("The streak lives still in our heart!") 28 | -------------------------------------------------------------------------------- /hackerearth/Sherlock and Date.cpp: -------------------------------------------------------------------------------- 1 | //CODE ARENA PLAYER 2 | // Problem Link : https://www.hackerearth.com/practice/basic-programming/implementation/basics-of-implementation/practice-problems/algorithm/sherlock-and-date/ 3 | 4 | #include 5 | using namespace std; 6 | 7 | //fnction to get previous date 8 | void getPrevDate(int *day,int *month, int *year) 9 | { 10 | //if there is first day of month 11 | if(*day==1) 12 | { 13 | //months which have 30 days in previous month 14 | if(*month==4|| *month==6|| *month==9|| *month==11) 15 | { 16 | *day=31; 17 | *month = *month -1; 18 | } 19 | //for MARCH, to define february last day 20 | else if(*month==3) 21 | { 22 | if(*year%400==0 || *year%4==0) 23 | *day=29; 24 | else 25 | *day=28; 26 | 27 | *month = *month -1; 28 | } 29 | //for January, to define December last day 30 | else if(*month==1) 31 | { 32 | *day=31; 33 | *month = 12; 34 | *year = *year - 1 ; 35 | } 36 | //for Feb, to define January last day 37 | else if(*month==2) 38 | { 39 | *day=31; 40 | *month = *month -1; 41 | } 42 | //for other months 43 | else 44 | { 45 | *day=30; 46 | *month = *month -1; 47 | } 48 | } 49 | //other days of month 50 | else 51 | { 52 | *day = *day-1; 53 | } 54 | 55 | } 56 | 57 | int main() 58 | { 59 | int t; 60 | cin >> t; 61 | string arr[12] = {"January", "February", "March", "April", "May", "June", 62 | "July", "August", "September", "October", "November" , "December"}; 63 | while(t--){ 64 | 65 | //date, assigning day, month and year 66 | int dd=0,mm=0,yy=0; 67 | string mm1; 68 | cin >> dd >> mm1 >> yy; 69 | for(int i=0; i<12; i++){ 70 | if(arr[i] == mm1){ 71 | mm = i+1;break; 72 | } 73 | } 74 | //function call to get previous date 75 | getPrevDate(&dd,&mm, &yy); 76 | //printing previous date 77 | 78 | cout< 7 | 8 | using namespace std; 9 | 10 | #define IO ios_base::sync_with_stdio(false);cin.tie(NULL); 11 | 12 | typedef long long ll; 13 | 14 | const int N = 1e5 + 5; 15 | const int MOD = 1e9 + 7; 16 | 17 | int main() { 18 | int tt; cin >> tt; 19 | while(tt--) { 20 | int k; cin >> k; 21 | string st; cin >> st; 22 | 23 | int ln = st.length(), f[ln]; 24 | for (int i = 0; i < ln; ++i) 25 | { 26 | f[i] = (st[i] - 'a') + 1; 27 | } 28 | unordered_map ump; 29 | int res = 0, curr = 0; 30 | for (int i = 0; i < ln; ++i) 31 | { 32 | curr += f[i]; 33 | 34 | if (curr == k)++res; 35 | if (ump.find(curr-k)!=ump.end())res+=ump[curr-k]; 36 | ++ump[curr]; 37 | } 38 | cout << res << endl; 39 | } 40 | 41 | return 0; 42 | } 43 | 44 | // 1 2 3 4 5 6 45 | -------------------------------------------------------------------------------- /hackerearth/Xenny and K-Equal-Triplets.cpp: -------------------------------------------------------------------------------- 1 | // CODE AREANA PLAYER 2 | // Probelem Link : https://www.hackerearth.com/problem/algorithm/xenny-and-k-equal-triplets/ 3 | 4 | #pragma Gpref optimize("O3") 5 | //#pragma Gpref optimize("Ofast") 6 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 7 | //#pragma Gpref optimize("unroll-loops") 8 | #include 9 | #define int unsigned long long 10 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 11 | #define TC int t; cin >> t; while(t--) 12 | #define FR(i,j,k) for(int i=j; i=k; --i) 14 | #define pb push_back 15 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 16 | #define go continue 17 | #define F first 18 | #define S second 19 | using namespace std; 20 | 21 | 22 | const int MOD = (int)1e9+7; 23 | const int N = 104; 24 | vector fact; 25 | 26 | int nCr(int n, int k) 27 | { 28 | int C[k+1]; 29 | memset(C, 0, sizeof(C)); 30 | 31 | C[0] = 1; 32 | 33 | for (int i = 1; i <= n; i++) 34 | { 35 | for (int j = min(i, k); j > 0; j--) 36 | C[j] = C[j] + C[j-1]; 37 | } 38 | return C[k]; 39 | } 40 | 41 | void reduceFraction(int x, int y) 42 | { 43 | int d; 44 | d = __gcd(x, y); 45 | 46 | x = x / d; 47 | y = y / d; 48 | 49 | cout << x << "/" << y << endl; 50 | } 51 | int32_t main(){ 52 | IOS; 53 | TC{ 54 | int n,k,count = 0; 55 | cin >> n >> k; 56 | vector v(n); 57 | FR(i,0,n)cin >> v[i]; 58 | int a = nCr(n, 3); 59 | FR(i,0,n){ 60 | if(v[i] == k)count++; 61 | } 62 | 63 | int b = nCr(count, 3); 64 | 65 | reduceFraction(b,a); 66 | } 67 | return 0; 68 | } 69 | 70 | -------------------------------------------------------------------------------- /hackerearth/marks_time.cpp: -------------------------------------------------------------------------------- 1 | /* nuttela Code Arena Player */ 2 | 3 | 4 | #pragma Gpref optimize("O3") 5 | //#pragma Gpref optimize("Ofast") 6 | //#pragma Gpref target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 7 | //#pragma Gpref optimize("unroll-loops") 8 | #include 9 | #define int long long 10 | #define ld long double 11 | #define ull unsigned long long 12 | #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); 13 | #define TC int t; cin >> t; while(t--) 14 | #define FR(i,j,k) for(int i=j; i=k; --i) 17 | #define BFRI(i,j,k,l) for(int i=j; i>=k; i-=l) 18 | #define pb push_back 19 | #define mp make_pair 20 | #define vi vector 21 | #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " 22 | #define pi pair 23 | #define F first 24 | #define S second 25 | 26 | 27 | using namespace std; 28 | const int MOD = (int)1e9+7; 29 | const int N = 1e5+1; 30 | const int M = 32; 31 | 32 | int modmul(int a,int b){int res=0;a%=MOD;while(b){if(b&1)res=(res+a)%MOD;a=(a<<1)%MOD;b>>=1;}return res;} 33 | int power(int x,unsigned int y){int res=1;x=x%MOD;while(y>0){if(y&1)res=(res*x)%MOD;y=y>>1;x=(x*x)%MOD;}return res;} 34 | int modexpo(int a,int b){int res=1;a%=MOD;while(b>0){if(b%2==1)res=(res*a)%MOD;a=(a*a)%MOD;b/=2;}return res%MOD;} 35 | int nCr(int n,int r){int res=1;if(r>n>>1)r=n-r;FR(i,0,r){res=(res*(n-i))%MOD;res=(res*modexpo(i+1,MOD-2))%MOD;}return res;} 36 | 37 | 38 | clock_t time_c=clock(); 39 | 40 | void nuttela() 41 | { 42 | time_c = clock()-time_c; 43 | cerr<< " Time : "<<(float)(time_c)/CLOCKS_PER_SEC<<"\n"; 44 | } 45 | 46 | bool is(int n) 47 | { 48 | if(n==0) 49 | return false; 50 | 51 | return (ceil(log2(n)) == floor(log2(n))); 52 | } 53 | int phi(int n) { 54 | int result = n; 55 | for (int i = 2; i * i <= n; i++) { 56 | if(n % i == 0) { 57 | while(n % i == 0) 58 | n /= i; 59 | result -= result / i; 60 | } 61 | } 62 | if(n > 1) 63 | result -= result / n; 64 | return result; 65 | } 66 | int32_t main(){ 67 | //Online JUDGE starts 68 | #ifndef ONLINE_JUDGE 69 | freopen("input.txt", "r", stdin); 70 | freopen("output.txt", "w", stdout); 71 | freopen("error.txt", "w", stderr); 72 | #endif 73 | //Online JUDGE ends32 74 | 75 | IOS; 76 | 77 | //pre(); 78 | //TC{ 79 | //CODE BEGINS 80 | int n, q; 81 | cin >> n >> q; 82 | vi time(n), marks(n), f; 83 | multimap > value; 84 | FR(i,0,n) cin >> time[i]; 85 | FR(i,0,n) cin >> marks[i]; 86 | 87 | FR(i,0,n) value.insert({marks[i],time[i]}); 88 | for(auto &x:value)f.pb(x.S); 89 | 90 | FR(i,1,n)f[i] += f[i-1]; 91 | 92 | while(q--){ 93 | int k; cin >> k; 94 | cout << f[k-1]; cout << '\n'; 95 | } 96 | 97 | //} 98 | nuttela(); 99 | 100 | return 0; 101 | } 102 | -------------------------------------------------------------------------------- /matrix_mul.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namepsace std; 4 | 5 | int mcm(int p[], int n){ 6 | int m[n][n]; 7 | int i, j, k, L, q; 8 | for (i = 1; i < n; i++) 9 | m[i][i] = 0; 10 | for (L=2; L