├── BST ├── bst.cpp └── bst.exe ├── Binary Tree-2 ├── binary_tree_2.cpp ├── binary_tree_2.exe └── input.txt ├── Binary Tree ├── binary_tree.cpp ├── binary_tree.exe ├── input.txt ├── temp.cpp └── tree_basic.cpp ├── Bitmasking ├── count_bits.cpp ├── n_queen.cpp ├── n_queen.cppp ├── uniq_2.cpp └── uniq_3.cpp ├── DP-1 ├── coins.cpp ├── coins.exe ├── fib.cpp ├── fib.exe ├── ladder_ways.cpp ├── ladder_ways.exe ├── min_steps_to_one.cpp └── min_steps_to_one.exe ├── DP3 ├── wines.cpp └── wines.exe ├── Divide & Conq ├── cows.cpp └── paratha.cpp ├── Graphs ├── New Text Document.txt ├── graph.cpp ├── graph_moon.cpp ├── graph_weighted.cpp └── readme.md.txt ├── Hashing ├── hash_test.cpp ├── hash_test.exe ├── hashtable.exe └── hashtable.h ├── Heaps-2 ├── input.txt ├── max_k_running_stream.cpp └── max_k_running_stream.exe ├── OOPS ├── classes_and_objects.cpp ├── classes_and_objects.exe ├── complex.cpp ├── linked_list.cpp ├── linked_list.exe └── test.py ├── Recursion-1 ├── fact.cpp ├── fibonaci.cpp ├── find_seven.cpp ├── inc_dec.cpp ├── power_fn.cpp ├── replace_pi.cpp └── sorted_array.cpp ├── Recursion-2 ├── bubble_sort.cpp ├── generate_strings.cpp ├── inv_cnt.cpp ├── merge_sort.cpp ├── permutation.cpp ├── subsequnces.cpp └── subsequnces.exe ├── Recursion-3 ├── generate_brackets.cpp ├── ladder_problem.cpp ├── nqueen.cpp ├── phone_keypad.cpp └── rat_in_maze.cpp └── queue ├── cars_sort.cpp ├── cars_sort.exe ├── hashmap.cpp ├── iniput.txt ├── output.txt ├── pair.cpp ├── pair.exe ├── queue.h ├── queue_stl.cpp ├── queue_stl.exe ├── queue_test.cpp ├── queue_test.exe ├── read_till_eof.cpp ├── read_till_eof.exe ├── redudant_paranthesis.cpp ├── stl.cpp ├── stl.exe ├── string_tokenizer.cpp └── string_tokenizer.exe /BST/bst.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | class Node{ 8 | public: 9 | int data; 10 | Node*left; 11 | Node*right; 12 | 13 | Node(int d){ 14 | data = d; 15 | left = right = NULL; 16 | } 17 | 18 | }; 19 | //Recursively Build the Tree 20 | Node* buildTree(Node*root){ 21 | int d; 22 | cin>>d; 23 | //Base Case 24 | if(d==-1){ 25 | return NULL; 26 | } 27 | 28 | 29 | //Rec Case 30 | root = new Node(d); 31 | root->left = buildTree(root->left); 32 | root->right = buildTree(root->right); 33 | return root; 34 | } 35 | //Print 36 | void preorderPrint(Node*root){ 37 | //Base Case 38 | if(root==NULL){ 39 | return; 40 | } 41 | 42 | //Rec Case 43 | cout<data<<" "; 44 | preorderPrint(root->left); 45 | preorderPrint(root->right); 46 | 47 | } 48 | int height(Node*root){ 49 | return root==NULL?0: 1+max(height(root->left),height(root->right)); 50 | } 51 | int sum(Node*root){ 52 | return root==NULL?0: root->data + sum(root->left) + sum(root->right); 53 | } 54 | 55 | int replaceBySum(Node*root){ 56 | //Base Case 57 | if(root==NULL){ 58 | return 0; 59 | } 60 | if(root->left==NULL and root->right==NULL){ 61 | return root->data; 62 | } 63 | int temp = root->data; 64 | root->data = replaceBySum(root->left) + replaceBySum(root->right); 65 | return root->data + temp; 66 | } 67 | int diameter(Node*root){ 68 | //Base Case 69 | if(root==NULL){ 70 | return 0; 71 | } 72 | 73 | //Rec Case 74 | int op1 = height(root->left) + height(root->right); 75 | int op2 = max(diameter(root->left),diameter(root->right)); 76 | return max(op1,op2); 77 | } 78 | class Pair{ 79 | public: 80 | int height; 81 | int diam; 82 | }; 83 | 84 | Pair diameterOpt(Node*root){ 85 | //Base Case 86 | Pair p; 87 | if(root==NULL){ 88 | p.height = p.diam = 0; 89 | return p; 90 | } 91 | 92 | //rec case 93 | Pair L,R; 94 | L = diameterOpt(root->left); 95 | R = diameterOpt(root->right); 96 | 97 | p.height = max(L.height,R.height) + 1; 98 | int op1 = L.height + R.height; 99 | int op2 = max(L.diam,R.diam); 100 | p.diam = max(op1,op2); 101 | return p; 102 | } 103 | 104 | class PairH{ 105 | public: 106 | int height; 107 | bool balance; 108 | }; 109 | 110 | PairH isHtBalanced(Node*root){ 111 | //Base Case 112 | PairH p; 113 | if(root==NULL){ 114 | p.height = 0; 115 | p.balance = true; 116 | return p; 117 | } 118 | //Rec Case 119 | PairH L,R; 120 | L = isHtBalanced(root->left); 121 | R = isHtBalanced(root->right); 122 | 123 | p.height = max(L.height,R.height) + 1; 124 | if(abs(L.height-R.height)<=1 and L.balance and R.balance){ 125 | p.balance = true; 126 | } 127 | else{ 128 | p.balance = false; 129 | } 130 | return p; 131 | }; 132 | 133 | void printAtLevelK(Node*root,int k){ 134 | //Base Case 135 | if(root==NULL){ 136 | return; 137 | } 138 | if(k==1){ 139 | cout<data<<" "; 140 | return; 141 | } 142 | //rec cas 143 | printAtLevelK(root->left,k-1); 144 | printAtLevelK(root->right,k-1); 145 | return; 146 | } 147 | void printLevelWise(Node*root){ 148 | int ht = height(root); 149 | for(int i=1;i<=ht;i++){ 150 | printAtLevelK(root,i); 151 | cout< q; 158 | q.push(root); 159 | q.push(NULL); 160 | 161 | while(!q.empty()){ 162 | 163 | Node* f = q.front(); 164 | if(f==NULL){ 165 | cout<data<<" "; 174 | q.pop(); 175 | if(f->left!=NULL){ 176 | q.push(f->left); 177 | } 178 | if(f->right!=NULL){ 179 | q.push(f->right); 180 | } 181 | } 182 | } 183 | } 184 | Node* insertInBST(Node*root,int d){ 185 | if(root==NULL){ 186 | root = new Node(d); 187 | return root; 188 | } 189 | //otherwise 190 | if(ddata){ 191 | root->left = insertInBST(root->left,d); 192 | } 193 | else{ 194 | root->right = insertInBST(root->right,d); 195 | } 196 | return root; 197 | } 198 | 199 | 200 | Node* buildTree(){ 201 | Node* root = NULL; 202 | int d; 203 | cin>>d; 204 | while(d!=-1){ 205 | root = insertInBST(root,d); 206 | cin>>d; 207 | } 208 | return root; 209 | } 210 | 211 | Node*arr2bst(int a[],int s,int e){ 212 | //Base Case 213 | if(s>e){ 214 | return NULL; 215 | } 216 | int mid = (s+e)/2; 217 | Node*root = new Node(a[mid]); 218 | root->left = arr2bst(a,s,mid-1); 219 | root->right = arr2bst(a,mid+1,e); 220 | return root; 221 | } 222 | 223 | bool search(Node*root,int key){ 224 | //base case 225 | if(root==NULL){ 226 | return false; 227 | } 228 | if(root->data==key){ 229 | return true; 230 | } 231 | if(keydata){ 232 | return search(root->left,key); 233 | } 234 | return search(root->right,key); 235 | } 236 | 237 | class LL{ 238 | public: 239 | Node*head; 240 | Node*tail; 241 | }; 242 | 243 | LL tree2LL(Node*root){ 244 | 245 | LL l; 246 | if(root==NULL){ 247 | l.head = l.tail = NULL; 248 | return l; 249 | } 250 | if(root->left==NULL and root->right==NULL){ 251 | l.head = l.tail = root; 252 | } 253 | else if(root->left!=NULL and root->right==NULL){ 254 | LL leftLL = tree2LL(root->left); 255 | leftLL.tail->right = root; 256 | l.head = leftLL.head; 257 | l.tail = root; 258 | } 259 | else if(root->left==NULL and root->right!=NULL){ 260 | LL rightLL = tree2LL(root->right); 261 | root->right = rightLL.head; 262 | l.head = root; 263 | l.tail = rightLL.tail; 264 | } 265 | else{ 266 | LL leftLL = tree2LL(root->left); 267 | LL rightLL = tree2LL(root->right); 268 | leftLL.tail->right = root; 269 | root->right = rightLL.head; 270 | l.head = leftLL.head; 271 | l.tail = rightLL.tail; 272 | } 273 | return l; 274 | 275 | } 276 | 277 | 278 | 279 | Node* removeNode(Node*root,int key){ 280 | if(root==NULL){ 281 | return NULL; 282 | } 283 | if(root->data==key){ 284 | //This is the node to be deleted 285 | //3 cases - 0 , 1, 2 children 286 | //1. leaf node 287 | if(root->left ==NULL and root->right==NULL){ 288 | delete root; 289 | return NULL; 290 | } 291 | else if(root->left!=NULL and root->right==NULL){ 292 | Node*temp = root->left; 293 | delete root; 294 | return temp; 295 | } 296 | else if(root->right!=NULL and root->left==NULL){ 297 | Node*temp = root->right; 298 | delete root; 299 | return temp; 300 | } 301 | else{ 302 | //find inorder successor 303 | Node*temp = root->right; 304 | while(temp->left!=NULL){ 305 | temp = temp->left; 306 | } 307 | //copy the temp data to root 308 | root->data = temp->data; 309 | //rec delete the temp->data from right subtree 310 | root->right = removeNode(root->right,temp->data); 311 | return root; 312 | } 313 | 314 | } 315 | else if(keydata){ 316 | root->left = removeNode(root->left,key); 317 | } 318 | else{ 319 | root->right = removeNode(root->right,key); 320 | } 321 | return root; 322 | 323 | 324 | 325 | 326 | 327 | } 328 | 329 | 330 | int main(){ 331 | 332 | 333 | Node* root = NULL; 334 | //root = buildTree(); 335 | //levelOrder(root); 336 | int arr[] = {1,2,3,4,5,6,7}; 337 | int n = sizeof(arr)/sizeof(int); 338 | 339 | root = arr2bst(arr,0,n-1); 340 | levelOrder(root); 341 | LL l = tree2LL(root); 342 | Node*temp = l.head; 343 | while(temp!=NULL){ 344 | cout<data<<"-->"; 345 | temp = temp->right; 346 | } 347 | 348 | //root = removeNode(root,5); 349 | //root = removeNode(root,4); 350 | 351 | 352 | 353 | 354 | return 0; 355 | } -------------------------------------------------------------------------------- /BST/bst.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/algo-aug2019/94a6387cc05673a883e8b0a1a7b0d31478dc40db/BST/bst.exe -------------------------------------------------------------------------------- /Binary Tree-2/binary_tree_2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | 9 | class Node{ 10 | public: 11 | int data; 12 | Node*left; 13 | Node*right; 14 | 15 | Node(int d){ 16 | data = d; 17 | left = right = NULL; 18 | } 19 | 20 | }; 21 | //Recursively Build the Tree 22 | Node* buildTree(Node*root){ 23 | int d; 24 | cin>>d; 25 | //Base Case 26 | if(d==-1){ 27 | return NULL; 28 | } 29 | 30 | 31 | //Rec Case 32 | root = new Node(d); 33 | root->left = buildTree(root->left); 34 | root->right = buildTree(root->right); 35 | return root; 36 | } 37 | //Print 38 | void preorderPrint(Node*root){ 39 | //Base Case 40 | if(root==NULL){ 41 | return; 42 | } 43 | 44 | //Rec Case 45 | cout<data<<" "; 46 | preorderPrint(root->left); 47 | preorderPrint(root->right); 48 | 49 | } 50 | int height(Node*root){ 51 | return root==NULL?0: 1+max(height(root->left),height(root->right)); 52 | } 53 | int sum(Node*root){ 54 | return root==NULL?0: root->data + sum(root->left) + sum(root->right); 55 | } 56 | 57 | int replaceBySum(Node*root){ 58 | //Base Case 59 | if(root==NULL){ 60 | return 0; 61 | } 62 | if(root->left==NULL and root->right==NULL){ 63 | return root->data; 64 | } 65 | int temp = root->data; 66 | root->data = replaceBySum(root->left) + replaceBySum(root->right); 67 | return root->data + temp; 68 | } 69 | int diameter(Node*root){ 70 | //Base Case 71 | if(root==NULL){ 72 | return 0; 73 | } 74 | 75 | //Rec Case 76 | int op1 = height(root->left) + height(root->right); 77 | int op2 = max(diameter(root->left),diameter(root->right)); 78 | return max(op1,op2); 79 | } 80 | class Pair{ 81 | public: 82 | int height; 83 | int diam; 84 | }; 85 | 86 | Pair diameterOpt(Node*root){ 87 | //Base Case 88 | Pair p; 89 | if(root==NULL){ 90 | p.height = p.diam = 0; 91 | return p; 92 | } 93 | 94 | //rec case 95 | Pair L,R; 96 | L = diameterOpt(root->left); 97 | R = diameterOpt(root->right); 98 | 99 | p.height = max(L.height,R.height) + 1; 100 | int op1 = L.height + R.height; 101 | int op2 = max(L.diam,R.diam); 102 | p.diam = max(op1,op2); 103 | return p; 104 | } 105 | 106 | class PairH{ 107 | public: 108 | int height; 109 | bool balance; 110 | }; 111 | 112 | PairH isHtBalanced(Node*root){ 113 | //Base Case 114 | PairH p; 115 | if(root==NULL){ 116 | p.height = 0; 117 | p.balance = true; 118 | return p; 119 | } 120 | //Rec Case 121 | PairH L,R; 122 | L = isHtBalanced(root->left); 123 | R = isHtBalanced(root->right); 124 | 125 | p.height = max(L.height,R.height) + 1; 126 | if(abs(L.height-R.height)<=1 and L.balance and R.balance){ 127 | p.balance = true; 128 | } 129 | else{ 130 | p.balance = false; 131 | } 132 | return p; 133 | }; 134 | 135 | void printAtLevelK(Node*root,int k){ 136 | //Base Case 137 | if(root==NULL){ 138 | return; 139 | } 140 | if(k==1){ 141 | cout<data<<" "; 142 | return; 143 | } 144 | //rec cas 145 | printAtLevelK(root->left,k-1); 146 | printAtLevelK(root->right,k-1); 147 | return; 148 | } 149 | void printLevelWise(Node*root){ 150 | int ht = height(root); 151 | for(int i=1;i<=ht;i++){ 152 | printAtLevelK(root,i); 153 | cout< q; 160 | q.push(root); 161 | q.push(NULL); 162 | 163 | while(!q.empty()){ 164 | 165 | Node* f = q.front(); 166 | if(f==NULL){ 167 | cout<data<<" "; 176 | q.pop(); 177 | if(f->left!=NULL){ 178 | q.push(f->left); 179 | } 180 | if(f->right!=NULL){ 181 | q.push(f->right); 182 | } 183 | } 184 | } 185 | } 186 | 187 | void root2Leaf(Node*root,vector &v){ 188 | //Base Case 189 | if(root==NULL){ 190 | return; 191 | } 192 | //Leaf Node 193 | if(root->left==NULL and root->right==NULL){ 194 | v.push_back(root->data); 195 | for(int i=0;idata); 204 | root2Leaf(root->left,v); 205 | root2Leaf(root->right,v); 206 | //Backtracking 207 | v.pop_back(); 208 | return; 209 | } 210 | 211 | void verticalOrderPrint(Node*root,int d,map > m){ 212 | //base case 213 | if(root==NULL){ 214 | return; 215 | } 216 | m[d].push_back(root->data); 217 | verticalOrderPrint(root->left,d-1,m); 218 | verticalOrderPrint(root->right,d+1,m); 219 | return; 220 | } 221 | 222 | Node* levelInputToTree(){ 223 | int data; 224 | cin>>data; 225 | Node*root = new Node(data); 226 | queue q; 227 | q.push(root); 228 | 229 | while(!q.empty()){ 230 | Node*f = q.front(); 231 | q.pop(); 232 | 233 | int c1,c2; 234 | cin>>c1>>c2; 235 | 236 | if(c1!=-1){ 237 | f->left = new Node(c1); 238 | q.push(f->left); 239 | } 240 | if(c2!=-1){ 241 | f->right = new Node(c2); 242 | q.push(f->right); 243 | } 244 | } 245 | return root; 246 | } 247 | 248 | int printAtKDist(Node*root,int target_data,int k){ 249 | //Leaf Node 250 | if(root==NULL){ 251 | return -1; 252 | } 253 | 254 | if(root->data==target_data){ 255 | printAtLevelK(root,k); 256 | return 0; 257 | } 258 | //Search for the Node in the Left Substree 259 | int l = printAtKDist(root->left,target_data,k); 260 | if(l!=-1){ 261 | int d = l; 262 | if(d+1==k){ 263 | cout<data<<" "; 264 | } 265 | else{ 266 | printAtLevelK(root->right,k-d-2); 267 | } 268 | return d+1; 269 | } 270 | int r = printAtKDist(root->left,target_data,k); 271 | else if(r!=-1){ 272 | int d = r; 273 | if(d+1==k){ 274 | cout<data<<" "; 275 | } 276 | else{ 277 | printAtLevelK(root->left,k-d-2); 278 | } 279 | return d+1; 280 | } 281 | return -1; 282 | } 283 | 284 | class Pair{ 285 | public: 286 | int inc; 287 | int exc; 288 | 289 | Pair(){ 290 | inc = exc = 0; 291 | } 292 | } 293 | 294 | Pair maxSumSubset(Node*root){ 295 | //base case 296 | Pair p; 297 | if(root==NULL){ 298 | return p; 299 | } 300 | //rec case 301 | Pair L = maxSumSubset(root->left); 302 | Pair R = maxSumSubset(root->right); 303 | 304 | p.inc = root->data + L.exc + R.exc; 305 | p.exc = max(L.inc,L.exc) + max(R.inc,R.exc); 306 | return p; 307 | } 308 | 309 | 310 | 311 | int main(){ 312 | Node* root = NULL; 313 | root = buildTree(root); 314 | printLevelWise(root); 315 | 316 | vector v; 317 | root2Leaf(root,v); 318 | 319 | //Hashmap 320 | map > m; 321 | 322 | verticalOrderPrint(root,0,m); 323 | 324 | //Iterate over all keys of hashmap from min to max 325 | for(auto p:m){ 326 | int key = p.first; 327 | 328 | cout<"; 329 | for(auto element:p.second){ 330 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | class Node{ 8 | public: 9 | int data; 10 | Node*left; 11 | Node*right; 12 | 13 | Node(int d){ 14 | data = d; 15 | left = right = NULL; 16 | } 17 | 18 | }; 19 | //Recursively Build the Tree 20 | Node* buildTree(Node*root){ 21 | int d; 22 | cin>>d; 23 | //Base Case 24 | if(d==-1){ 25 | return NULL; 26 | } 27 | 28 | 29 | //Rec Case 30 | root = new Node(d); 31 | root->left = buildTree(root->left); 32 | root->right = buildTree(root->right); 33 | return root; 34 | } 35 | //Print 36 | void preorderPrint(Node*root){ 37 | //Base Case 38 | if(root==NULL){ 39 | return; 40 | } 41 | 42 | //Rec Case 43 | cout<data<<" "; 44 | preorderPrint(root->left); 45 | preorderPrint(root->right); 46 | 47 | } 48 | int height(Node*root){ 49 | return root==NULL?0: 1+max(height(root->left),height(root->right)); 50 | } 51 | int sum(Node*root){ 52 | return root==NULL?0: root->data + sum(root->left) + sum(root->right); 53 | } 54 | 55 | int replaceBySum(Node*root){ 56 | //Base Case 57 | if(root==NULL){ 58 | return 0; 59 | } 60 | if(root->left==NULL and root->right==NULL){ 61 | return root->data; 62 | } 63 | int temp = root->data; 64 | root->data = replaceBySum(root->left) + replaceBySum(root->right); 65 | return root->data + temp; 66 | } 67 | int diameter(Node*root){ 68 | //Base Case 69 | if(root==NULL){ 70 | return 0; 71 | } 72 | 73 | //Rec Case 74 | int op1 = height(root->left) + height(root->right); 75 | int op2 = max(diameter(root->left),diameter(root->right)); 76 | return max(op1,op2); 77 | } 78 | class Pair{ 79 | public: 80 | int height; 81 | int diam; 82 | }; 83 | 84 | Pair diameterOpt(Node*root){ 85 | //Base Case 86 | Pair p; 87 | if(root==NULL){ 88 | p.height = p.diam = 0; 89 | return p; 90 | } 91 | 92 | //rec case 93 | Pair L,R; 94 | L = diameterOpt(root->left); 95 | R = diameterOpt(root->right); 96 | 97 | p.height = max(L.height,R.height) + 1; 98 | int op1 = L.height + R.height; 99 | int op2 = max(L.diam,R.diam); 100 | p.diam = max(op1,op2); 101 | return p; 102 | } 103 | 104 | class PairH{ 105 | public: 106 | int height; 107 | bool balance; 108 | }; 109 | 110 | PairH isHtBalanced(Node*root){ 111 | //Base Case 112 | PairH p; 113 | if(root==NULL){ 114 | p.height = 0; 115 | p.balance = true; 116 | return p; 117 | } 118 | //Rec Case 119 | PairH L,R; 120 | L = isHtBalanced(root->left); 121 | R = isHtBalanced(root->right); 122 | 123 | p.height = max(L.height,R.height) + 1; 124 | if(abs(L.height-R.height)<=1 and L.balance and R.balance){ 125 | p.balance = true; 126 | } 127 | else{ 128 | p.balance = false; 129 | } 130 | return p; 131 | }; 132 | 133 | void printAtLevelK(Node*root,int k){ 134 | //Base Case 135 | if(root==NULL){ 136 | return; 137 | } 138 | if(k==1){ 139 | cout<data<<" "; 140 | return; 141 | } 142 | //rec cas 143 | printAtLevelK(root->left,k-1); 144 | printAtLevelK(root->right,k-1); 145 | return; 146 | } 147 | void printLevelWise(Node*root){ 148 | int ht = height(root); 149 | for(int i=1;i<=ht;i++){ 150 | printAtLevelK(root,i); 151 | cout< q; 158 | q.push(root); 159 | q.push(NULL); 160 | 161 | while(!q.empty()){ 162 | 163 | Node* f = q.front(); 164 | if(f==NULL){ 165 | cout<data<<" "; 174 | q.pop(); 175 | if(f->left!=NULL){ 176 | q.push(f->left); 177 | } 178 | if(f->right!=NULL){ 179 | q.push(f->right); 180 | } 181 | } 182 | } 183 | } 184 | 185 | 186 | 187 | int main(){ 188 | Node* root = NULL; 189 | root = buildTree(root); 190 | preorderPrint(root); 191 | 192 | PairH p = isHtBalanced(root); 193 | if(p.balance){ 194 | cout<<"Tree is height balanced"; 195 | } 196 | else{ 197 | cout<<"Not Height Balanced!"; 198 | } 199 | cout< 2 | using namespace std; 3 | 4 | int cntBits(int n){ 5 | int cnt = 0; 6 | while(n>0){ 7 | cnt = cnt + (n&1); 8 | n = n>>1; 9 | } 10 | return cnt; 11 | } 12 | 13 | int cntBitsFast(int n){ 14 | int cnt=0; 15 | while(n>0){ 16 | n = n&(n-1); 17 | cnt++; 18 | } 19 | return cnt; 20 | } 21 | int getIthBit(int n,int i){ 22 | return (n>>i)&1; 23 | } 24 | void setIthBit(int &n,int i){ 25 | n = (n|(1<>n; 32 | cout< 2 | using namespace std; 3 | 4 | int nqueen(int n,int row,int ld,int rd){ 5 | //base case 6 | DONE = (1<0){ 16 | int p = pos&(-pos); 17 | pos -= p; 18 | ans += nqueen(n,row|p,(ld|p)<<1,(rd|p)>>1); 19 | } 20 | return ans; 21 | } 22 | 23 | int main(){ 24 | int n; 25 | cin>>n; 26 | cout< 2 | using namespace std; 3 | 4 | void findUniq2(int *arr,int n){ 5 | //First Step 6 | int xor_res = 0; 7 | for(int i=0;i>1; 16 | } 17 | //seprte those numbers from array which have 1 in jth position 18 | int mask = (1<0){ 22 | a = a^arr[i]; 23 | } 24 | } 25 | //finally 26 | int b = xor_res^a; 27 | cout< 2 | using namespace std; 3 | 4 | 5 | int findUniq3(int *arr,int n){ 6 | 7 | int freq[64] = {0}; 8 | 9 | for(int i=0;i0){ 14 | int last_bit = (no&1); 15 | if(last_bit==1){ 16 | freq[j]++; 17 | } 18 | j++; 19 | no = no>>1; 20 | } 21 | } 22 | //Take Mod 3 23 | int p=1; 24 | int ans=0; 25 | for(int i=0;i<64;i++){ 26 | int digit= freq[i]%3; 27 | ans = ans + p*digit; 28 | p = p<<1; 29 | } 30 | return ans; 31 | } 32 | 33 | int main(){ 34 | 35 | int arr[] = {3,1,2,3,1,2,4,3,1,2}; 36 | int n = sizeof(arr)/sizeof(int); 37 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int minCoinsChange(int coins[],int m,int n,int *dp){ 7 | //Base Case 8 | if(n==0){ 9 | return 0; 10 | } 11 | //Rec Case 12 | if(dp[n]!=0){ 13 | return dp[n]; 14 | } 15 | int ans = INT_MAX; 16 | //Let make a call every subprob 17 | for(int j=0;j=0){ 19 | int subprob = minCoinsChange(coins,m,n-coins[j],dp) + 1; 20 | if(subprob=0){ 35 | int subprob = dp[n-coins[j]]+1; 36 | if(subprob 2 | using namespace std; 3 | //Top Down DP - Recursion + Memoisation 4 | int fib(int n,int dp[]){ 5 | //base case 6 | if(n==0 or n==1){ 7 | return n; 8 | } 9 | //already computed 10 | if(dp[n]!=0){ 11 | return dp[n]; 12 | } 13 | 14 | //recursive case 15 | int ans = fib(n-1,dp) + fib(n-2,dp); 16 | return dp[n] = ans; 17 | } 18 | 19 | //Bottom Up DP - Iterative 20 | int fibBU(int n){ 21 | int dp[100] = {0}; 22 | dp[1] = 1; 23 | 24 | for(int i=2;i<=n;i++){ 25 | dp[i] = dp[i-1] + dp[i-2]; 26 | } 27 | return dp[n]; 28 | } 29 | 30 | 31 | 32 | int main(){ 33 | int dp[100] = {0}; 34 | int n; 35 | cin>>n; 36 | cout< 2 | using namespace std; 3 | 4 | int ways(int n,int k,int dp[]){ 5 | //base case 6 | if(n==0){ 7 | return 1; 8 | } 9 | //-- 10 | if(dp[n]!=0){ 11 | return dp[n]; 12 | } 13 | 14 | //rec case 15 | int ans = 0; 16 | for(int jump=1;jump<=k;jump++){ 17 | ans += (n-jump>=0? ways(n-jump,k,dp):0); 18 | } 19 | return dp[n] = ans; 20 | } 21 | 22 | //DP Optimised -HW 23 | /* 24 | dp[n] = 2*dp[n-1] - dp[n-k-1] if n>=k+1 e 25 | = 2*dp[n-1] otherwise for n>=2 26 | dp[0] = dp[1] = 1 27 | */ 28 | 29 | 30 | int main(){ 31 | 32 | int n; 33 | int k; 34 | int dp[100] = {0}; 35 | cin>>n>>k; 36 | cout< 2 | #include 3 | using namespace std; 4 | 5 | int minSteps(int n,int dp[]){ 6 | //base case 7 | if(n==1){ 8 | return 0; 9 | } 10 | //rec case 11 | if(dp[n]!=0){ 12 | return dp[n]; 13 | } 14 | int op1,op2,op3; 15 | op1 = minSteps(n-1,dp); 16 | op2 = n%2==0? minSteps(n/2,dp):INT_MAX; 17 | op3 = n%3==0? minSteps(n/3,dp):INT_MAX; 18 | int ans = min(op1,min(op2,op3)) + 1; 19 | return dp[n] = ans; 20 | } 21 | //Try to make bottom up code! 22 | 23 | int minStepsBU(int n){ 24 | int dp[100] = {0}; 25 | dp[1] = 0; 26 | //Loop 27 | for(int i=2;i<=n;i++){ 28 | int op1 = dp[i-1]; 29 | int op2 = i%2==0?dp[i/2]:INT_MAX; 30 | int op3 = i%3==0?dp[i/3]:INT_MAX; 31 | int ans = min(op1,min(op2,op3)) + 1; 32 | dp[i] = ans; 33 | } 34 | return dp[n]; 35 | } 36 | 37 | int main(){ 38 | int dp[100] = {0}; 39 | int n; 40 | cin>>n; 41 | cout< 2 | using namespace std; 3 | 4 | int dp[10][10] = {0}; 5 | 6 | int wines(int n,int arr[],int i,int j,int y){ 7 | //base case 8 | if(i>j){ 9 | return 0; 10 | } 11 | if(dp[i][j]!=0){ 12 | return dp[i][j]; 13 | } 14 | //rec case 15 | int op1 = arr[i]*y + wines(n,arr,i+1,j,y+1); 16 | int op2 = arr[j]*y + wines(n,arr,i,j-1,y+1); 17 | return dp[i][j] = max(op1,op2); 18 | } 19 | 20 | 21 | int main(){ 22 | int win[ ] = {3,1,5}; 23 | int n = 3; 24 | wines(n,win,0,n-1,1); 25 | 26 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | bool canPlace(int c,int n,int *stalls,int sep){ 6 | 7 | int cows=1; 8 | int loc = stalls[0]; 9 | 10 | //remanining cows ko place krna hai 11 | for(int i=1;i=sep){ 14 | cows++; 15 | loc = x; 16 | if(cows==c){ 17 | return true; 18 | } 19 | } 20 | } 21 | 22 | return false; 23 | } 24 | 25 | 26 | int main(){ 27 | int n,c; 28 | cin>>n>>c; 29 | 30 | int stalls[100]; 31 | for(int i=0;i>stalls[i]; 33 | } 34 | //Sort Array upto N Elements 35 | sort(stalls,stalls+n); 36 | 37 | //Define Binary Search 38 | int s = 0; 39 | int e = stalls[n-1] - stalls[0]; 40 | 41 | int mid; 42 | int ans = 0; 43 | while(s<=e){ 44 | mid = (s+e)>>1; 45 | if(canPlace(c,n,stalls,mid)){ 46 | ans = mid; 47 | s = mid + 1; 48 | } 49 | else{ 50 | e = mid - 1; 51 | } 52 | } 53 | cout< 2 | using namespace std; 3 | 4 | void canMakeParatha(int C,int *cook,int T,int P){ 5 | 6 | int total_paratha = 0; 7 | for(int i=0;i=P){ 16 | return true; 17 | } 18 | 19 | } 20 | return false; 21 | } 22 | 23 | int main(){ 24 | 25 | int cook[] = {1,2,3,4}; 26 | int C = 4; 27 | int P = 10; 28 | int T = 0; 29 | 30 | int s = 0; 31 | int e = INT_MAX; // R*(P)*(P+1)/2; 32 | 33 | while(s<=e){ 34 | int mid = (s+e)/2; 35 | if(canMakeParatha(C,cook,mid,P)){ 36 | T = mid; 37 | e = mid -1; 38 | } 39 | else{ 40 | s = mid+1; 41 | } 42 | } 43 | 44 | 45 | 46 | 47 | return 0; 48 | } -------------------------------------------------------------------------------- /Graphs/New Text Document.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/algo-aug2019/94a6387cc05673a883e8b0a1a7b0d31478dc40db/Graphs/New Text Document.txt -------------------------------------------------------------------------------- /Graphs/graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | template 8 | class Graph{ 9 | 10 | unordered_map > m; 11 | 12 | public: 13 | Graph(){ 14 | 15 | } 16 | void addEdge(T u,T v,bool bidir=true){ 17 | m[u].push_back(v); 18 | if(bidir){ 19 | m[v].push_back(u); 20 | } 21 | } 22 | void print(){ 23 | for(auto p:m){ 24 | T src = p.first; 25 | list l = p.second; 26 | 27 | cout<"; 28 | for(auto dest:l){ 29 | cout< &visited){ 36 | //.....Define the DFS Logic........ 37 | visited[node] = true; 38 | cout< visited; 49 | 50 | 51 | for(auto city_pair:m){ 52 | T city = city_pair.first; 53 | if(!visited[city]){ 54 | cout<<"Cluster : " 55 | dfsVisit(city,visited); 56 | cout< q; 62 | q.push(start); 63 | unordered_map visited; 64 | visited[start] = true; 65 | 66 | while(!q.empty()){ 67 | T front = q.front(); 68 | cout< g; 85 | g.addEdge("Delhi","Amritsar"); 86 | g.addEdge("Delhi","Jaipur"); 87 | g.addEdge("Delhi","Lucknow"); 88 | g.addEdge("Lucknow","Patna"); 89 | g.addEdge("Lucknow","Bhopal"); 90 | g.addEdge("Jaipur","Bhopal"); 91 | g.addEdge("Mumbai","Pune") 92 | g.addEdge("Mumbai","Nagpur") 93 | 94 | 95 | g.bfs("Delhi"); 96 | 97 | 98 | //g.dfs(); 99 | g.print(); 100 | 101 | 102 | return 0; 103 | } -------------------------------------------------------------------------------- /Graphs/graph_moon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | template 7 | class Graph{ 8 | 9 | unordered_map > m; 10 | int N; 11 | public: 12 | Graph(int v){ 13 | N = v; 14 | } 15 | void addEdge(T u,T v,bool bidir=true){ 16 | m[u].push_back(v); 17 | if(bidir){ 18 | m[v].push_back(u); 19 | } 20 | } 21 | void print(){ 22 | for(auto p:m){ 23 | T src = p.first; 24 | list l = p.second; 25 | 26 | cout<"; 27 | for(auto dest:l){ 28 | cout< &visited){ 35 | //.....Define the DFS Logic........ 36 | visited[node] = true; 37 | cout< visited; 50 | int finalans = N*(N-1)/2; 51 | 52 | for(auto city_pair:m){ 53 | T city = city_pair.first; 54 | if(!visited[city]){ 55 | cout<<"Cluster : "; 56 | int ans = dfsVisit(city,visited); 57 | finalans -= ans*(ans-1)/2; 58 | } 59 | } 60 | return finalans; 61 | 62 | } 63 | }; 64 | 65 | 66 | int main(){ 67 | 68 | Graph g(5); 69 | g.addEdge(0,1); 70 | g.addEdge(1,4); 71 | g.addEdge(2,3); 72 | 73 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | template 7 | class Graph{ 8 | 9 | unordered_map > > m; 10 | 11 | public: 12 | Graph(){ 13 | 14 | } 15 | void addEdge(T u,T v,int wt,bool bidir=true){ 16 | m[u].push_back(make_pair(v,wt)); 17 | if(bidir){ 18 | m[v].push_back(make_pair(u,wt)); 19 | } 20 | } 21 | void print(){ 22 | for(auto p:m){ 23 | T src = p.first; 24 | auto l = p.second; 25 | 26 | cout<"; 27 | for(auto dest_p:l){ 28 | cout< g; 39 | g.addEdge("Delhi","Amritsar",1); 40 | g.addEdge("Delhi","Jaipur",2); 41 | g.addEdge("Delhi","Lucknow",3); 42 | g.addEdge("Lucknow","Patna",4); 43 | g.addEdge("Lucknow","Bhopal",4); 44 | g.addEdge("Jaipur","Bhopal",5); 45 | g.print(); 46 | 47 | 48 | return 0; 49 | } -------------------------------------------------------------------------------- /Graphs/readme.md.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/algo-aug2019/94a6387cc05673a883e8b0a1a7b0d31478dc40db/Graphs/readme.md.txt -------------------------------------------------------------------------------- /Hashing/hash_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "hashtable.h" 3 | using namespace std; 4 | 5 | int main(){ 6 | Hashtable h; 7 | h.insert("Mango",100); 8 | h.insert("Apple",50); 9 | h.insert("Guava",40); 10 | h.insert("banana",30); 11 | h.insert("litchi",20); 12 | h.insert("kiwi",10); 13 | 14 | int * price = h.search("banana"); 15 | if(price==NULL){ 16 | cout<<"banana not found"; 17 | } 18 | else{ 19 | cout<<"Banana costs "<<*price< 2 | #include 3 | using namespace std; 4 | 5 | template 6 | class Node{ 7 | public: 8 | string key; 9 | T value; 10 | Node* next; 11 | 12 | Node(string k,T v){ 13 | key = k; 14 | value = v; 15 | next = NULL; 16 | } 17 | ~Node(){ 18 | if(next!=NULL){ 19 | delete next; 20 | } 21 | } 22 | }; 23 | 24 | template 25 | class Hashtable{ 26 | Node **table; 27 | int cs; 28 | int ts; 29 | int hashfn(string key){ 30 | int idx = 0; 31 | int c = 1; 32 | 33 | for(int j=0;j** oldTable = table; 42 | table = new Node*[2*ts+1]; //Prime - HW 43 | int oldts = ts; 44 | ts = 2*ts+1; 45 | cs = 0; 46 | //init the new table with null ptrs 47 | for(int i=0;i*temp = oldTable[i]; 53 | 54 | while(temp!=NULL){ 55 | string k = temp->key; 56 | T v = temp->value; 57 | insert(k,v); 58 | temp = temp->next; 59 | } 60 | //remove the ith linked list 61 | if(oldTable[i]!=NULL){ 62 | delete oldTable[i]; 63 | } 64 | } 65 | delete [] oldTable; 66 | } 67 | 68 | public: 69 | Hashtable(int ds=7){ 70 | cs = 0; 71 | ts = ds; 72 | table = new Node*[ts]; 73 | //Init 74 | for(int i=0;i* n = new Node(k,val); 81 | 82 | //Insert at head of linked list at idx position 83 | n->next = table[idx]; 84 | table[idx] = n; 85 | 86 | cs++; 87 | float load_factor = cs/float(ts); 88 | if(load_factor>0.7){ 89 | rehash(); 90 | } 91 | 92 | } 93 | void print(){ 94 | //Iterate over buckets 95 | for(int i=0;i"; 97 | Node* temp = table[i]; 98 | while(temp!=NULL){ 99 | cout<key<<"->"; 100 | temp = temp->next; 101 | } 102 | cout<*temp = table[idx]; 108 | while(temp!=NULL){ 109 | if(temp->key==key){ 110 | return &temp->value; 111 | } 112 | temp = temp->next; 113 | } 114 | return NULL; //otherwise 115 | } 116 | void erase(string key){ 117 | //Delete that node containing key,val pair 118 | // Delete from LL 119 | //Homework 120 | 121 | } 122 | //Operator Overloading 123 | T& operator[](string key){ 124 | T* p = search(key); 125 | if(p==NULL){ 126 | T garbage; 127 | insert(key,garbage); 128 | p = search(key); 129 | } 130 | return *p; 131 | } 132 | 133 | 134 | }; 135 | 136 | -------------------------------------------------------------------------------- /Heaps-2/input.txt: -------------------------------------------------------------------------------- 1 | 1 2 3 8 -1 6 9 10 -1 11 0 1 2 3 5 -1 2 | 3 | 0 1 1 2 3 5 8 9 10 11 4 | -------------------------------------------------------------------------------- /Heaps-2/max_k_running_stream.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | void printHeap(priority_queue, greater > h){ 7 | //Copy Empty Print 8 | while(!h.empty()){ 9 | cout<, greater > h; 21 | int cs = 0; 22 | 23 | while(scanf("%d",&no)!=EOF){ 24 | if(no==-1){ 25 | //Query 26 | printHeap(h); 27 | continue; 28 | } 29 | 30 | if(cs h.top()){ 35 | //Push by Replacement 36 | h.pop(); 37 | h.push(no); 38 | } 39 | } 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Heaps-2/max_k_running_stream.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/algo-aug2019/94a6387cc05673a883e8b0a1a7b0d31478dc40db/Heaps-2/max_k_running_stream.exe -------------------------------------------------------------------------------- /OOPS/classes_and_objects.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Car{ 5 | private: 6 | int price; 7 | public: 8 | string model; 9 | char *number_plate; 10 | //Default Constructor 11 | Car(){ 12 | number_plate = NULL; 13 | } 14 | //Params Constructors 15 | Car(string m,int p){ 16 | model = m; 17 | price =p; 18 | number_plate = new char[5]; 19 | strcpy(number_plate,"ABC1"); 20 | } 21 | //Copy Constructor 22 | 23 | Car(Car &x){ 24 | //cout<<"Overwritten Copy Constructor"; 25 | model = x.model; 26 | price = x.price; 27 | //Deep Copy 28 | number_plate = new char[strlen(x.number_plate)+1]; 29 | strcpy(number_plate,x.number_plate); 30 | } 31 | 32 | //Copy Assignment Operator(Operator Loading) 33 | void operator=(const Car &x){ 34 | //cout<<"In copy asssingment operator"; 35 | model = x.model; 36 | price = x.price; 37 | } 38 | void operator!() const{ 39 | cout<0){ 55 | price = p; 56 | } 57 | } 58 | 59 | void print() const{ 60 | cout<<"Car Model "<>(istream& i, Car &x){ 77 | string m; 78 | int p; 79 | cin>>m>>p; 80 | x.update(m,p); 81 | return i; 82 | } 83 | 84 | int fun(){ 85 | int x; 86 | x = 5; 87 | return x; 88 | } 89 | 90 | //Function Overloading 91 | //Constructor Overloading 92 | 93 | /* 94 | //Class has these 4 member functions by default 95 | - Constructor 96 | - Copy Constructor 97 | - Copy Assignment Operator 98 | - Destructor 99 | */ 100 | 101 | int main(){ 102 | 103 | Car c1("Audi",100); 104 | Car c2(c1); 105 | 106 | /* 107 | // !c1; 108 | // c1["Raju"]; 109 | //Car c3(c1); 110 | Car c3 = c1; 111 | Car c4; //-->Default Constructor 112 | c4 = c3; //--> Copy Assignment Operator (Hidden) 113 | //cin>>c1>>c2; 114 | cout<>c1>>c2; 8 | cout< 2 | using namespace std; 3 | 4 | class Node{ 5 | public: 6 | int data; 7 | Node*next; 8 | 9 | Node(int d){ 10 | data = d; 11 | next = NULL; 12 | } 13 | ~Node(){ 14 | if(next!=NULL){ 15 | delete next; 16 | } 17 | 18 | } 19 | }; 20 | 21 | 22 | //Linked List 23 | /* 24 | class LinkedList{ 25 | Node*head; 26 | LinkedList(){ 27 | head = NULL; 28 | } 29 | void addNode(){ 30 | ... 31 | } 32 | 33 | } 34 | */ 35 | 36 | void insertAtHead(Node* &head,int d){ 37 | if(head==NULL){ 38 | head = new Node(d); 39 | return; 40 | } 41 | Node* n = new Node(d); 42 | n->next = head; 43 | head = n; 44 | } 45 | void print(Node *head){ 46 | while(head!=NULL){ 47 | cout<data<<"-->"; 48 | head = head->next; 49 | } 50 | cout<next; 61 | } 62 | Node*n = new Node(d); 63 | n->next = t->next; 64 | t->next = n; 65 | return; 66 | } 67 | 68 | void deleteInMiddle(Node*&head,int d,int p){ 69 | 70 | 71 | //Hint - 72 | // 73 | 74 | } 75 | 76 | void reverse(Node*&head){ 77 | Node*C = head; 78 | Node*P = NULL; 79 | Node* N = C->next; 80 | 81 | while(C!=NULL){ 82 | N = C->next; 83 | C->next = P; 84 | P = C; 85 | C = N; 86 | } 87 | head = P; 88 | return; 89 | } 90 | //Recursively Reverse (O(N)) 91 | Node* reverseRec(Node*head){ 92 | //Base Case 93 | if(head->next==NULL){ 94 | return head; 95 | } 96 | //Rec Case 97 | Node*chotaHead = reverseRec(head->next); 98 | 99 | head->next->next = head; 100 | head->next = NULL; 101 | return chotaHead; 102 | } 103 | 104 | // MidPoint 105 | Node*midpoint(Node*head){ 106 | Node*slow = head; 107 | Node* fast =head->next; 108 | 109 | while(fast!=NULL and fast->next!=NULL){ 110 | fast = fast->next->next; 111 | slow = slow->next; 112 | } 113 | return slow; 114 | } 115 | // Merge Two LinkedLists 116 | Node* merge(Node*a,Node*b){ 117 | if(a==NULL){ 118 | return b; 119 | } 120 | if(b==NULL){ 121 | return a; 122 | } 123 | //rec case 124 | Node* t; 125 | if(a->data < b->data){ 126 | t = a; 127 | t->next = merge(a->next,b); 128 | } 129 | else{ 130 | t = b; 131 | t->next = merge(a,b->next); 132 | } 133 | return t; 134 | } 135 | 136 | Node* mergeSort(Node *l){ 137 | //base case 138 | if(l==NULL or l->next==NULL){ 139 | return l; 140 | } 141 | //mid 142 | Node *mid = midpoint(l); 143 | Node*a = l; 144 | Node*b = mid->next; 145 | mid->next = NULL; 146 | 147 | a = mergeSort(a); 148 | b = mergeSort(b); 149 | 150 | return merge(a,b); 151 | } 152 | 153 | 154 | 155 | ostream& operator<<(ostream &o,Node*&head){ 156 | print(head); 157 | return o; 158 | } 159 | 160 | int main(){ 161 | 162 | Node *head = NULL; 163 | insertAtHead(head,5); 164 | insertAtHead(head,14); 165 | insertAtHead(head,2); 166 | insertAtHead(head,10); 167 | insertInMiddleNode(head,3,2); 168 | cout<31: 3 | return True 4 | else: 5 | return False 6 | 7 | def is_special(cardvalue): 8 | if cardvalue in [1,2,3]: 9 | return True 10 | 11 | return False 12 | 13 | 14 | class Achieve31(): 15 | 16 | #Init Cards 17 | def __init__(self): 18 | self.mincard = 0 19 | self.maxcard = 10 20 | self.cards = np.arange(self.mincard,self.maxcard+1) 21 | self.gamethreshold = 31 22 | self.reset() 23 | self.playersum = 0 24 | self.dealercard = 0 25 | self.dealersum = 0 26 | self.actions = (0,1) 27 | # First Card, Second Card, Third Card State 28 | # 0 - No Card Arrived, 1 - Card Arrived and Used 2 - Card Avbl not used 29 | 30 | self.special_cards = { 31 | 1:0, 32 | 2:0, 33 | 3:0 34 | } 35 | 36 | self.dealer_special_cards = { 37 | 1:0, 38 | 2:0, 39 | 3:0 40 | } 41 | 42 | 43 | def drawCard(self): 44 | cardvalue = np.random.choice(self.cards) 45 | 46 | p = np.random.random() 47 | if p<=2/3: 48 | #Black Card 49 | return cardvalue 50 | 51 | else: 52 | #Red Card 53 | return -cardvalue 54 | 55 | def sumHand(self,current_sum,special_cards): 56 | 57 | if current_sum>31 and special_cards[3]==2: 58 | special_cards[3] = 1 59 | current_sum -= 10 60 | 61 | elif current_sum>31 and special_cards[2]==2: 62 | special_cards[2] = 1 63 | current_sum -= 10 64 | 65 | elif current_sum>31 and special_cards[1]==2: 66 | special_cards[1] = 1 67 | current_sum -= 10 68 | 69 | 70 | elif special_cards[3]==1 and special_cards[2]==1 and current_sum+20<=31: 71 | current_sum += 20 72 | special_cards[3] = 2 73 | special_cards[2] = 2 74 | 75 | elif special_cards[2]==1 and special_cards[1]==1 and current_sum+20<=31: 76 | current_sum += 20 77 | special_cards[2] = 2 78 | special_cards[1] = 2 79 | 80 | elif special_cards[3]==1 and special_cards[1]==1 and current_sum+20<=31: 81 | current_sum += 20 82 | special_cards[3] = 2 83 | special_cards[1] = 2 84 | 85 | elif special_cards[3]==1 and current_sum + 10 <= 31: 86 | current_sum +=10 87 | special_cards[3] = 2 88 | 89 | elif special_cards[2]==1 and current_sum + 10 <= 31: 90 | current_sum +=10 91 | special_cards[2] = 2 92 | 93 | elif special_cards[1]==1 and current_sum + 10 <= 31: 94 | current_sum +=10 95 | special_cards[1] = 2 96 | 97 | return current_sum,special_cards 98 | 99 | def getObservation(self): 100 | player_sum, self.special_cards = self.sumHand(self.playersum,self.special_cards) 101 | return player_sum, self.dealercard,self.special_cards 102 | 103 | 104 | def reset(self): 105 | card1 = self.drawCard() 106 | card2 = self.drawCard() 107 | 108 | self.playersum = card1 109 | self.dealercard = card2 110 | self.dealercard += card2 111 | 112 | if is_special(card1): 113 | self.special_cards[card1] = 1 114 | 115 | if is_special(card2): 116 | self.dealer_special_cards[card2] = 1 117 | 118 | return self.getObservation() 119 | 120 | 121 | def step(self,action): 122 | terminated = None 123 | reward = None 124 | 125 | if action==1: 126 | card = self.drawCard() 127 | print(card) 128 | self.playersum += card 129 | 130 | if is_special(card) and self.special_card[card]==0: 131 | self.special_card[card] = 1 132 | 133 | self.playersum,self.special_cards = self.sumHand(self.playersum,self.special_cards) 134 | 135 | if is_bust(self.playersum): 136 | terminated = True 137 | reward = -1 138 | else: 139 | terminated = False 140 | reward = 0 141 | 142 | elif action==0: 143 | terminated = True 144 | self.dealer,self.dealer_special_cards = sumHand(self.dealersum,self.dealer_special_cards) 145 | 146 | #Dealer Game Play according to Fixed Policy 147 | while s>0 and s<25: 148 | card += self.drawCard() 149 | if is_special(card) and self.dealer_special_cards[card]==0: 150 | self.dealer_special_cards[card] = 1 151 | 152 | self.dealersum += card 153 | self.dealersum,self.dealer_special_cards = sumHand(self.dealersum,self.dealer_special_cards) 154 | 155 | # Compare Scores for Final Reward 156 | ds = self.dealersum 157 | ps = self.playersum 158 | 159 | if ds<0 or ds>self.gamethreshold or dsps: 164 | reward = -1 165 | 166 | state = self.getObservation() 167 | return state,reward,terminated -------------------------------------------------------------------------------- /Recursion-1/fact.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fact(int n){ 5 | //Base Case 6 | if(n==0){ 7 | return 1; 8 | } 9 | //Rec Case 10 | int subProb = fact(n-1); //Assuming 11 | return n*subProb; 12 | } 13 | 14 | int main(){ 15 | int n; 16 | cin>>n; 17 | cout< 2 | using namespace std; 3 | 4 | int fib(int n){ 5 | if(n==0 or n==1){ 6 | return n; 7 | } 8 | return fib(n-1) + fib(n-2); 9 | } 10 | 11 | 12 | int main(){ 13 | int n; 14 | cin>>n; 15 | cout< 2 | using namespace std; 3 | 4 | int first_7(int arr[],int n){ 5 | //Base Case 6 | if(n==0){ 7 | return -1; 8 | } 9 | //Current Or Remaning Array 10 | if(arr[0]==7){ 11 | return 0; 12 | } 13 | 14 | int idx = first_7(arr+1,n-1); 15 | if(idx!=-1){ 16 | return idx+1; 17 | } 18 | return -1; 19 | } 20 | int last_7(int arr[],int n){ 21 | if(n==0){ 22 | return -1; 23 | } 24 | //Current Or Remaning Array 25 | int idx= last_7(arr+1,n-1); 26 | if(idx!=-1){ 27 | return idx + 1; 28 | } 29 | 30 | if(arr[0]==7){ 31 | return 0; 32 | } 33 | return -1; 34 | } 35 | 36 | int main(){ 37 | 38 | int arr[] = {7,7,7,7,7}; 39 | int n = sizeof(arr)/sizeof(int); 40 | cout< 2 | using namespace std; 3 | 4 | void inc(int n){ 5 | if(n==0){ 6 | return; 7 | } 8 | inc(n-1); 9 | cout<>n; 26 | inc(n); 27 | dec(n); 28 | 29 | 30 | return 0; 31 | } -------------------------------------------------------------------------------- /Recursion-1/power_fn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int multiply(int a,int b){ 5 | if(b==0){ 6 | return 0; 7 | } 8 | return a + multiply(a,b-1); 9 | } 10 | int power(int a,int b){ 11 | if(b==0){ 12 | return 1; 13 | } 14 | return a*power(a,b-1); 15 | } 16 | int fastPower(int a,int b){ 17 | if(b==0){ 18 | return 1; 19 | } 20 | //Rec Case 21 | int subProb = fastPower(a,b/2); 22 | subProb = subProb*subProb; 23 | if(b%2==0){ 24 | return subProb; 25 | } 26 | return a*subProb; 27 | 28 | } 29 | 30 | int main(){ 31 | int a,b; 32 | cin>>a>>b; 33 | cout< 2 | using namespace std; 3 | 4 | void replacePi(char a[],int i){ 5 | //Base Case 6 | if(a[i]=='\0'){ 7 | return; 8 | } 9 | //Rec Case 10 | if(a[i]=='p' and a[i+1]=='i'){ 11 | int j=i+2; 12 | while(a[j]!='\0'){ 13 | j++; 14 | } 15 | //Shifting 16 | while(j>=i+2){ 17 | a[j+2] = a[j]; 18 | j--; 19 | } 20 | a[i] = '3'; 21 | a[i+1] ='.'; 22 | a[i+2] = '1'; 23 | a[i+3] = '4'; 24 | replacePi(a,i+4); 25 | return; 26 | } 27 | replacePi(a,i+1); 28 | return; 29 | 30 | } 31 | 32 | int main(){ 33 | char a[100]; 34 | cin>>a; 35 | replacePi(a,0); 36 | cout< 2 | using namespace std; 3 | 4 | //Array - > Sorted or Not! 5 | 6 | bool isSorted(int a[],int n){ 7 | if(n==1){ 8 | return true; 9 | } 10 | //Rec Case 11 | if(a[0] 2 | using namespace std; 3 | 4 | void bubbleSort(int arr[],int n){ 5 | //base case 6 | if(n==1){ 7 | return; 8 | } 9 | //on entire array 10 | for(int i=0;i<=n-2;i++){ 11 | if(arr[i]>arr[i+1]){ 12 | swap(arr[i],arr[i+1]); 13 | } 14 | } 15 | bubbleSort(arr,n-1); 16 | 17 | } 18 | 19 | void bubbleSort2(int arr[],int i,int n){ 20 | //base case 21 | if(n==1){ 22 | return; 23 | } 24 | if(i==n-1){ 25 | bubbleSort2(arr,0,n-1); 26 | return; 27 | } 28 | if(arr[i]>arr[i+1]){ 29 | swap(arr[i],arr[i+1]); 30 | } 31 | bubbleSort2(arr,i+1,n); 32 | return; 33 | } 34 | 35 | int main(){ 36 | int arr[] = {5,4,3,1,0}; 37 | int n = sizeof(arr)/sizeof(int); 38 | bubbleSort2(arr,0,n); 39 | 40 | for(int i=0;i 2 | using namespace std; 3 | 4 | void generateStrings(char *input,char *output,int i,int j){ 5 | //base case 6 | if(input[i]=='\0'){ 7 | output[j] = '\0'; 8 | cout<>input; 31 | generateStrings(input,output,0,0); 32 | 33 | 34 | 35 | return 0; 36 | } -------------------------------------------------------------------------------- /Recursion-2/inv_cnt.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int merge(int arr[],int s,int e){ 5 | int mid = (s+e)/2; 6 | int i=s; 7 | int j = mid+1; 8 | int k = s; 9 | int temp[100]; 10 | int cnt = 0; 11 | while(i<=mid and j<=e){ 12 | if(arr[i] A (Copy back to original array a) 31 | for(int x=s;x<=e;x++){ 32 | arr[x] = temp[x]; 33 | } 34 | return cnt; 35 | } 36 | 37 | int mergeSort(int arr[],int s,int e){ 38 | //base case 39 | if(s>=e){ 40 | return 0; 41 | } 42 | 43 | //rec case 44 | //1. Divide into parts 45 | int mid = (s+e)/2; 46 | 47 | //2. Rec Sort the Two Parts 48 | int x = mergeSort(arr,s,mid); 49 | int y = mergeSort(arr,mid+1,e); 50 | 51 | //3. Merge the two parts 52 | return x+y+merge(arr,s,e); 53 | } 54 | 55 | int main(){ 56 | int arr[] = {5,4,3,1,0}; 57 | int n = sizeof(arr)/sizeof(int); 58 | cout< 2 | using namespace std; 3 | 4 | void merge(int arr[],int s,int e){ 5 | int mid = (s+e)/2; 6 | int i=s; 7 | int j = mid+1; 8 | int k = s; 9 | int temp[100]; 10 | 11 | while(i<=mid and j<=e){ 12 | if(arr[i] A (Copy back to original array a) 30 | for(int x=s;x<=e;x++){ 31 | arr[x] = temp[x]; 32 | } 33 | return; 34 | } 35 | 36 | void mergeSort(int arr[],int s,int e){ 37 | //base case 38 | if(s>=e){ 39 | return; 40 | } 41 | 42 | //rec case 43 | //1. Divide into parts 44 | int mid = (s+e)/2; 45 | 46 | //2. Rec Sort the Two Parts 47 | mergeSort(arr,s,mid); 48 | mergeSort(arr,mid+1,e); 49 | 50 | //3. Merge the two parts 51 | return merge(arr,s,e); 52 | } 53 | 54 | int main(){ 55 | int arr[] = {5,4,3,1,0}; 56 | int n = sizeof(arr)/sizeof(int); 57 | mergeSort(arr,0,n-1); 58 | 59 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | void permute(char *input,int i,set &s){ 6 | //Base Case 7 | if(input[i]=='\0'){ 8 | string temp(input); 9 | s.insert(temp); 10 | //cout< s; 24 | 25 | char input[100]; 26 | cin>>input; 27 | permute(input,0,s); 28 | 29 | for(string k:s){ 30 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | bool compare(string s1,string s2){ 8 | int l1 = s1.length(); 9 | int l2 = s2.length(); 10 | 11 | if(l1 &v){ 24 | //base case 25 | if(input[i]=='\0'){ 26 | output[j] = '\0'; 27 | //cout< v; 43 | 44 | cin>>input; 45 | subseq(input,output,0,0,v); 46 | 47 | //All strings are stored in the vector 48 | sort(v.begin(),v.end(),compare); 49 | for(int i=0;i 2 | using namespace std; 3 | 4 | void generate_brackets(char *output,int open,int close,int i,int n){ 5 | if(i==2*n){ 6 | cout<>n; 27 | char output[100]; 28 | generate_brackets(output,0,0,0,n); 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Recursion-3/ladder_problem.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | int ways(int n,int k){ 6 | if(n==0){ 7 | return 1; 8 | } 9 | if(n<0){ 10 | return 0; 11 | } 12 | int ans = 0; 13 | for(int jump=1;jump<=k;jump++){ 14 | ans += ways(n-jump,k); 15 | } 16 | return ans; 17 | } 18 | 19 | int main(){ 20 | int n,k; 21 | cin>>n>>k; 22 | cout< 2 | using namespace std; 3 | 4 | bool isSafe(int board[][10],int i,int j,int n){ 5 | //column queen 6 | for(int k=0;k=0 and y>=0){ 13 | if(board[x][y]==1) 14 | return false; 15 | x--; 16 | y--; 17 | } 18 | //right diagonal 19 | x=i,y=j; 20 | while(x>=0 and y>n; 62 | cout< 2 | using namespace std; 3 | 4 | char keypad[][10] = {"","","ABC","DEF","GHI","JKL", 5 | "MNO","PQRS","TUV","WXYZ"}; 6 | 7 | void generate_keypad_strings(char number[],char output[],int i,int j){ 8 | //base case 9 | if(number[i]=='\0'){ 10 | output[j] = '\0'; 11 | cout<>number; 30 | generate_keypad_strings(number,output,0,0); 31 | 32 | return 0; 33 | } 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /Recursion-3/rat_in_maze.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int gridWays(char board[][10],int i,int j,int m,int n){ 5 | //BASE CASE 6 | if(i==m-1 and j==n-1){ 7 | board[m-1][n-1] = '1'; 8 | for(int x=0;x=m or j>=n){ 19 | return 0; 20 | } 21 | if(board[i][j]=='X'){ 22 | return 0; 23 | } 24 | //Mark the currently visited cell 25 | board[i][j] = '1'; 26 | 27 | int x = gridWays(board,i,j+1,m,n); 28 | int y = gridWays(board,i+1,j,m,n); 29 | 30 | //Unmark the currently visited cell (Backtracking) 31 | board[i][j] = '0'; 32 | return x+y; 33 | } 34 | 35 | int main(){ 36 | int m,n; 37 | char board[10][10] = { "0000X", 38 | "00X00", 39 | "000XX", 40 | "XX000", 41 | }; 42 | 43 | cout< 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | class Fruit{ 8 | public: 9 | string name; 10 | int price; 11 | Fruit(string f,int p){ 12 | name = f; 13 | price = p; 14 | } 15 | }; 16 | 17 | bool compare(Fruit a,Fruit b){ 18 | if(a.price==b.price){ 19 | return a.name < b.name; 20 | } 21 | return a.price>n; 29 | vector v; 30 | 31 | for(int i=0;i>n>>p; 34 | Fruit f(n,p); 35 | v.push_back(f); 36 | } 37 | 38 | //sort 39 | sort(v.begin(),v.end(),compare); 40 | //print 41 | for(int i=0;i 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | 9 | unordered_map m; 10 | //1. Insert 11 | m["Apple"] = 100; 12 | //2. Way 13 | m.insert(make_pair("Mango",120)); 14 | 15 | //3. Way 16 | pair p("Gauva",30); 17 | m.insert(p); 18 | 19 | //LookUp 20 | cout< 2 | #include 3 | using namespace std; 4 | 5 | class Triplet{ 6 | int a; 7 | int b; 8 | int c; 9 | }; 10 | 11 | bool compare(pair p1,pair p2){ 12 | return p1.second < p2.second; 13 | } 14 | 15 | int main(){ 16 | 17 | //Constructor 18 | pair p(10,20); 19 | 20 | cout< p1,p2; 24 | p1.first = "Mango"; 25 | p1.second = 100; 26 | 27 | cout< > v; 35 | v.push_back(p1); 36 | v.push_back(p2); 37 | 38 | //sort(...) 39 | 40 | } -------------------------------------------------------------------------------- /queue/pair.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/coding-blocks-archives/algo-aug2019/94a6387cc05673a883e8b0a1a7b0d31478dc40db/queue/pair.exe -------------------------------------------------------------------------------- /queue/queue.h: -------------------------------------------------------------------------------- 1 | 2 | template 3 | class Queue{ 4 | 5 | T *arr; 6 | int f; 7 | int r; 8 | int cs; 9 | int ms; 10 | 11 | public: 12 | Queue(int ds=5){ 13 | ms = ds; 14 | arr = new T[ms]; 15 | cs = 0; 16 | f = 0; 17 | r = ms - 1; 18 | } 19 | 20 | void push(T d){ 21 | if(cs==ms){ 22 | return; 23 | } 24 | r = (r+1)%ms; 25 | arr[r] = d; 26 | cs++; 27 | } 28 | void pop(){ 29 | if(cs==0){ 30 | return; 31 | } 32 | f = (f+1)%ms; 33 | cs--; 34 | } 35 | bool empty(){ 36 | return cs==0; 37 | } 38 | bool full(){ 39 | return cs==ms; 40 | } 41 | T front(){ 42 | return arr[f]; 43 | } 44 | }; -------------------------------------------------------------------------------- /queue/queue_stl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | 9 | queue q; 10 | for(int i=0;i<=7;i++){ 11 | q.push(65+i); 12 | } 13 | 14 | q.pop(); 15 | q.push(65+8); 16 | 17 | while(!q.empty()){ 18 | cout< 2 | #include "queue.h" 3 | using namespace std; 4 | 5 | class Person{ 6 | public: 7 | 8 | }; 9 | 10 | int main(){ 11 | 12 | Queue qp; 13 | Queue q; 14 | for(int i=0;i<=7;i++){ 15 | q.push(65+i); 16 | } 17 | 18 | q.pop(); 19 | q.push(65+8); 20 | 21 | while(!q.empty()){ 22 | cout< 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | int no; 9 | while(scanf("%d",&no)!=EOF){ 10 | cout< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | 9 | //-->Vector 10 | vector v; 11 | for(int i=0;i<=10;i++){ 12 | v.push_back(i); 13 | } 14 | v[0] = 100; 15 | 16 | // Print 17 | for(int i=0;i 2 | #include 3 | using namespace std; 4 | 5 | 6 | int main(){ 7 | 8 | char a[1000]; 9 | /* 10 | char ch = cin.get(); 11 | int i = 0; 12 | while(ch!='\n'){ 13 | //cout<