├── .gitignore ├── Books ├── CP Handbook.pdf ├── E-Maxx.pdf └── System Design Basics.pdf ├── Learn ├── C++ │ ├── README.md │ ├── cpp.md │ ├── custom comparator.md │ └── for loop.md ├── README.md ├── dynamic programming.md └── oop c++.md ├── Problems ├── 2 pointer │ ├── 1.md │ └── readme.md ├── README.md ├── array │ └── 1.md ├── bitmask dp │ └── readme.md ├── dynamic programming │ ├── 1.md │ ├── 2.md │ ├── 3_unsolved.md │ ├── 4.md │ ├── 5.md │ └── readme.md ├── fenwick tree │ ├── 1.md │ └── readme.md ├── graph tree │ ├── 1_unsolved.md │ ├── 2.md │ ├── 3_unsolved.md │ ├── 4.md │ ├── 5_unsolved.md │ ├── 6.md │ ├── 7.md │ └── readme.md ├── greedy │ ├── 1.md │ └── 2.md ├── implementation │ └── 1.md ├── linkedlist │ └── 1.md ├── lowest common ancestor │ └── readme.md ├── maths │ ├── 1.md │ ├── 2.md │ ├── 3_unsolved.md │ ├── 4_unsolved.md │ ├── 5_unsolved.md │ └── 6.md ├── prefix sum │ ├── 1.md │ ├── 2.md │ ├── 3.md │ ├── 4.md │ └── readme.md ├── problem_structure.md ├── random practice problems │ ├── atcoder.md │ ├── codeforces.md │ ├── hackerrank.md │ └── leetcode.md ├── segment tree │ ├── 1.cpp │ └── readme.md ├── sqrt decomposition + mo's algo │ └── readme.md ├── stack queue heap │ ├── 1.md │ └── 2.md ├── string │ ├── 1.md │ └── readme.md └── trie │ └── readme.md ├── Programming ├── 1.cpp ├── README.md ├── debug.h ├── default.cpp ├── diff.txt ├── e1.txt ├── i1.txt └── o1.txt ├── README.md ├── Scripts ├── README.md ├── diff.py ├── kill.bat └── run.py └── Sublime Text ├── Build ├── C++.sublime-build ├── Java.sublime-build ├── Javascript.sublime-build ├── Php.sublime-build └── Python3.sublime-build ├── Packages ├── C++AutoComplete │ ├── IO │ │ ├── basic_ios.sublime-completions │ │ ├── iomanip.sublime-completions │ │ ├── ios.sublime-completions │ │ ├── iostream.sublime-completions │ │ ├── ostream.sublime-completions │ │ └── streambuf.sublime-completions │ ├── classes │ │ ├── class.sublime-snippet │ │ ├── class_template.sublime-snippet │ │ ├── exception.sublime-snippet │ │ ├── struct.sublime-snippet │ │ └── struct_template.sublime-snippet │ ├── functions │ │ ├── constexpr_function.sublime-snippet │ │ ├── main_function.sublime-snippet │ │ ├── regular_function.sublime-snippet │ │ ├── templated_function.sublime-snippet │ │ ├── templated_function_auto.sublime-snippet │ │ ├── templated_function_auto_constexpr.sublime-snippet │ │ └── templated_function_constexpr.sublime-snippet │ ├── functors │ │ ├── binary_template_functor.sublime-snippet │ │ ├── functor.sublime-snippet │ │ └── unary_template_functor.sublime-snippet │ ├── headers │ │ ├── Cheader.sublime-completions │ │ └── CppHeader.sublime-completions │ ├── install.txt │ ├── lambda │ │ ├── catchallbyref.sublime-snippet │ │ ├── catchallbyvalue.sublime-snippet │ │ ├── full.sublime-snippet │ │ ├── lamret.sublime-snippet │ │ └── minimal.sublime-snippet │ ├── methods │ │ └── methods.sublime-completions │ ├── multithreading │ │ └── atomic.sublime-completions │ ├── preprocessor │ │ ├── clang_greater_version.sublime-snippet │ │ ├── clang_less.sublime-snippet │ │ ├── gcc_greater.sublime-snippet │ │ ├── gcc_less.sublime-snippet │ │ ├── if_elif_else_preprocessor.sublime-snippet │ │ ├── if_else_preprocessor.sublime-snippet │ │ └── ifndef.sublime-snippet │ └── utilities │ │ ├── declval.sublime-snippet │ │ ├── forward.sublime-snippet │ │ ├── mitlicense.sublime-snippet │ │ ├── namespace.sublime-snippet │ │ ├── template.sublime-snippet │ │ ├── try-catch.sublime-snippet │ │ ├── using_templated.sublime-snippet │ │ └── variadic_template.sublime-snippet ├── Custom │ ├── compare.py │ ├── file_explorer.py │ ├── quote.py │ └── terminal.py └── README.md ├── README.md └── Snippets ├── all.sublime-completions ├── bit-manipulation.sublime-snippet ├── boostlib.sublime-snippet ├── c++ function.sublime-snippet ├── c++.sublime-snippet ├── debug.sublime-snippet ├── for.sublime-snippet ├── function.sublime-snippet ├── graph.sublime-snippet ├── grid.sublime-snippet ├── hashing.sublime-snippet ├── in-range.sublime-snippet ├── input-output.sublime-snippet ├── lambda-comparator.sublime-snippet ├── modulo.sublime-snippet ├── n-dimensional vector.sublime-snippet ├── random-number.sublime-snippet └── to_string.sublime-snippet /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | *.exe 3 | -------------------------------------------------------------------------------- /Books/CP Handbook.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhiraj-01/CP/adf0286287830a63aa8797fd47a6c8cee4acc1b4/Books/CP Handbook.pdf -------------------------------------------------------------------------------- /Books/E-Maxx.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhiraj-01/CP/adf0286287830a63aa8797fd47a6c8cee4acc1b4/Books/E-Maxx.pdf -------------------------------------------------------------------------------- /Books/System Design Basics.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhiraj-01/CP/adf0286287830a63aa8797fd47a6c8cee4acc1b4/Books/System Design Basics.pdf -------------------------------------------------------------------------------- /Learn/C++/README.md: -------------------------------------------------------------------------------- 1 | ## C++ 2 | 3 | ### Learn 4 | - https://www.learncpp.com/ 5 | - https://devdocs.io/cpp/ 6 | 7 | ### Tips and Tricks 8 | - C++ tricks - https://codeforces.com/blog/entry/15643 9 | - C++ tips and tricks - https://codeforces.com/blog/entry/74684 10 | - C++17, competitive programming edition - https://codeforces.com/blog/entry/57729 11 | -------------------------------------------------------------------------------- /Learn/C++/cpp.md: -------------------------------------------------------------------------------- 1 | # C++ 2 | 3 | https://devdocs.io/cpp/ 4 | 5 | ### Hello World 6 | ```cpp 7 | #include 8 | using namespace std; 9 | 10 | int main() { 11 | cout << "Hello World" << endl; 12 | return 0; 13 | } 14 | ``` 15 | 16 | ### Data Types 17 | ```cpp 18 | int a = 1; 19 | // 32 bit => -2^31 to (2^31)-1 => ~ -1e9 to 1e9 20 | int MX = INT_MAX; 21 | int MN = INT_MIN; 22 | 23 | unsigned int x = 10; 24 | // only positive numbers 25 | // 0 to (2^32)-1 26 | 27 | long long int b = 1e17; 28 | // 64 bit => -2^64 to 2^64 => ~ -1e18 to 1e18 29 | long long int MX = LLONG_MAX; 30 | long long int MN = LLONG_MIN; 31 | 32 | char c = 'a'; 33 | // single character 34 | // 'a' = 97 (ASCII value), 'b' = 98 .. 35 | 36 | string s = "hello"; 37 | // string is not a data type, but a class 38 | // s.length(), s.size(), s.empty() ... 39 | 40 | double PI = 3.14; 41 | long double d = 1.23; 42 | float f = 2.1; 43 | 44 | auto any = "string"; 45 | any = "you can change it to other string"; 46 | any = 10; // not possible 47 | // you can assign any data type at initialization time 48 | 49 | auto x; 50 | x = 10; // error 51 | // The variable declared with auto keyword should be 52 | // initialized at the time of its declaration only or else 53 | // there will be a compile-time error 54 | 55 | // Tip: Always use long long int and long double (for cp) 56 | ``` 57 | 58 | ### Array 59 | ```cpp 60 | int a[10]; 61 | // 10 integers 62 | 63 | int a[10][10]; 64 | // a[i] is array of 10 integer 65 | 66 | string a[10]; 67 | // array of string 68 | 69 | int a; 70 | // single integer 71 | 72 | int arr[5]; 73 | // array of integers of size 5 74 | // we can access each integer by index arr[index] => int 75 | 76 | vector v(10); 77 | // vector of integers of size 10 78 | // we can access each integer by index v[index] => int 79 | 80 | vector v[10]; 81 | // array of vectors of integer of size 10 82 | // we can access each vector by index v[index] => vector of int 83 | 84 | vector> v(10); 85 | // vector of vector of integers of size 10 86 | // we can access each vector by index v[index] => vector of int 87 | ``` 88 | 89 | ### Pair 90 | ```cpp 91 | pair p; 92 | 93 | pair p{1, "ok"}; 94 | p.first = 10; 95 | p.second = "update"; 96 | 97 | // destructuring 98 | auto [x, s] = p; 99 | x = 10, s = "update"; 100 | ``` 101 | 102 | ### Vector 103 | ```cpp 104 | vector v; 105 | v.push_back(1); 106 | // O(1) 107 | // add new element (append) 108 | 109 | v.pop_back(); 110 | // O(1) 111 | // remove last element 112 | 113 | v.front(); 114 | // O(1) 115 | // v[0] = first element 116 | 117 | v.back(); 118 | // O(1) 119 | // v[v.size() - 1] = last element 120 | 121 | v.empty(); 122 | // O(1) 123 | // return true if empty 124 | 125 | v.size(); 126 | // O(1) 127 | // return size 128 | 129 | v.clear(); 130 | // O(1) 131 | // clear the vector 132 | 133 | v.resize(new_size, value = default value); 134 | // O(new_size) 135 | // v.resize(10) => new size of vector is 10 136 | // if new size < size, extra elements will be removed (value will not be set here) 137 | // else new elements will be added with default values 138 | 139 | v.assign(new_size, value); 140 | // O(new_size) 141 | // resize to new size and set given value 142 | 143 | /* 144 | begin() end() 145 | | | 146 | _ [ _ _ _ _ _ _ _ _ _ _ _ ] _ 147 | | | 148 | rend() rbegin() 149 | 150 | container 151 | */ 152 | 153 | v.begin(); 154 | // O(1) 155 | // iterator to first element 156 | // dereference to get value 157 | // v[0] == *v.begin(); 158 | 159 | v.end(); 160 | // O(1) 161 | // iterator to the element after the last element 162 | // v = [1, 2] v.end() point to next memory location after 2 163 | // v.back() == *(--v.end()); 164 | 165 | v.rend(); 166 | // O(1) 167 | // iterator to element before first element 168 | 169 | v.rbegin(); 170 | // O(1) 171 | // iterator to last element. 172 | 173 | v.erase(pos); // iterator to the element to remove 174 | // O(n) 175 | // remove element pointed by iterator 176 | // v.erase(v.begin()) remove first ele 177 | // v.erase(v.begin() + 1) remove 2nd ele 178 | 179 | v.erase(first, last) // range of elements to remove 180 | 181 | v.begin() + x 182 | // iterator to v[x] 183 | 184 | v.end() - x 185 | // iterator to v[v.size() - x] 186 | 187 | vector v(size); 188 | // O(size) 189 | // vector v(10); 190 | // vector of int size 10 191 | // default values 0 192 | 193 | vector v(size, value); 194 | // O(size) 195 | // vector v(10, 5); 196 | // default values 5, size 10 197 | 198 | vector v = {1, 3, 4, 2}; 199 | // list assignment 200 | 201 | vector new_v = v; 202 | // O(size of v) 203 | 204 | v[index] 205 | // O(1) 206 | // value at given index 207 | 208 | // comparition possible, inbuilt default 209 | // (vector1 == vector2) is correct C++ syntax. 210 | // There is an == operator for vectors. 211 | if(new_v == v) { 212 | cout << "both are same" << endl; 213 | } 214 | if(new_v < v) { 215 | // new_v is lexicographically smaller than v 216 | } 217 | 218 | // print 219 | // for each loop 220 | for(int x : v) { 221 | cout << x << endl; 222 | } 223 | // using index 224 | for(int i = 0; i < v.size(); i++) { 225 | cout << v[i] << " "; 226 | } 227 | // using iterator 228 | for(auto it = a.begin(); it != a.end(); it++) { 229 | cout << *it << endl; 230 | } 231 | // reverse order 232 | for(auto it = a.rbegin(); it != a.rend(); it++) { 233 | cout << *it << endl; 234 | } 235 | 236 | // *(v.begin() + x) = v[x] 237 | cout << (*(v.begin() + 2) == v[2]) << endl; 238 | ``` 239 | 240 | ### Stack 241 | ```cpp 242 | // last in first out (LIFO) 243 | // push => add element at the end of stack 244 | // pop => remove last element 245 | 246 | stack s; 247 | s.push(10); 248 | // O(1) 249 | // add new element to stack 250 | 251 | s.top(); 252 | // O(1) 253 | // get top (last) element 254 | 255 | s.pop(); 256 | // O(1) 257 | // remove top element (last element) 258 | 259 | s.empty(); 260 | // O(1) 261 | 262 | s.size(); 263 | // O(1) 264 | 265 | s.clear(); 266 | // error, not possible in stack 267 | 268 | // print 269 | while(!s.empty()) { 270 | cout << s.top() << endl; 271 | s.pop(); 272 | } 273 | ``` 274 | 275 | ### Queue 276 | ```cpp 277 | // first in first out (FIFO) 278 | // push => add element at the end of queue 279 | // pop => remove first element 280 | 281 | queue q; 282 | q.push(10); 283 | // O(1) 284 | // add new element to queue 285 | 286 | q.front(); 287 | // O(1) 288 | // get front element 289 | 290 | q.pop(); 291 | // O(1) 292 | // remove front element 293 | 294 | q.empty(); 295 | // O(1) 296 | 297 | q.size(); 298 | // O(1) 299 | 300 | q.clear(); 301 | // error, not possible in queue 302 | 303 | // print 304 | while(!q.empty()) { 305 | cout << q.front() << endl; 306 | q.pop(); 307 | } 308 | ``` 309 | 310 | ### Deque 311 | ```cpp 312 | // double ended queue 313 | // add front, add back 314 | // pop front, pop back 315 | 316 | deque q; 317 | q.push_front(10); 318 | // O(1) 319 | // add new ele. at front 320 | 321 | q.push_back(11); 322 | // O(1) 323 | // add new ele. at end 324 | 325 | q.pop_front(); 326 | // O(1) 327 | // remove first ele 328 | 329 | q.pop_back(); 330 | // O(1) 331 | // remove last ele 332 | 333 | q.front(); 334 | // O(1) 335 | // get front ele. 336 | 337 | q.back(); 338 | // O(1) 339 | // get back ele. 340 | 341 | q.empty(); 342 | // O(1) 343 | 344 | q.size(); 345 | // O(1) 346 | 347 | q.clear(); 348 | // O(1) 349 | // clear dequeue 350 | 351 | q[index] 352 | // O(1) 353 | // value at index 354 | 355 | // print 356 | - for each loop 357 | - using index 358 | - using iterator 359 | ``` 360 | 361 | ### Priority queue 362 | ```cpp 363 | Priority Queue (push, top, pop) 364 | Higher value at the top 365 | 366 | priority_queue pq; 367 | pq.push(value); 368 | // O(log(size of pq)) 369 | // add value to pq 370 | 371 | pq.top() 372 | // O(1) 373 | // return top element (higher value) 374 | 375 | pq.pop(); 376 | // O(log(size of pq)) 377 | // remove top element 378 | 379 | pq.size(); 380 | pq.empty(); 381 | // O(1) 382 | 383 | priority_queue pq; 384 | pq.push(10); 385 | pq.push(30); 386 | // pq = {30, 10} 387 | 388 | pq.top() = 30 389 | pq.pop() remove 30 390 | pq.top() = 10 391 | 392 | // min priority queue 393 | priority_queue, greater> pq; 394 | pq.push(10); 395 | pq.push(30); 396 | // pq = {10, 30} 397 | 398 | pq.clear(); // error, not possible 399 | pq.begin(), pq.end() .. // error, not possible 400 | 401 | // print 402 | while(!pq.empty()) { 403 | cout << pq.top() << endl; 404 | pq.pop(); 405 | } 406 | ``` 407 | 408 | ### String 409 | ```cpp 410 | string s = "Dhiraj"; 411 | s += '.'; // add char at the end 412 | s += " Govindvira"; // concat string at the end 413 | // s = "Dhiraj.Govindvira"; 414 | 415 | string s = "one"; 416 | string t = "two"; 417 | string p = s + "#" + t; 418 | // p = "one#two"; 419 | 420 | s.size(); 421 | // O(1) 422 | 423 | s.length(); 424 | // O(1) 425 | // same as size() 426 | 427 | s.front(); 428 | // O(1) 429 | // first character 430 | 431 | s.back(); 432 | // O(1) 433 | // last character 434 | 435 | s.push_back(char); 436 | // O(1) 437 | // add char. at the end 438 | 439 | s.pop_back(); 440 | // O(1) 441 | // remove last char. 442 | 443 | s.empty(); 444 | s.size(); 445 | s.clear(); 446 | s[index] 447 | // all operations O(1) 448 | 449 | // print 450 | - cout << s << endl; 451 | - for each 452 | - using index 453 | - using iterator 454 | ``` 455 | 456 | ### Set 457 | ```cpp 458 | Store unique values in sorted orders 459 | 460 | set s; 461 | 462 | s.insert(value); 463 | // O(log(size of set)) 464 | // insert value 465 | 466 | s.erase(value); 467 | // O(log(size of set)) 468 | // removes the element (if one exists) 469 | 470 | s.erase(iterator); 471 | // O(log(size of set)) 472 | // remove element pointed by iterator 473 | 474 | s.find(value); 475 | // O(log(size of set)) 476 | // return iterator 477 | { 478 | std::set example = {1, 2, 3, 4}; 479 | auto search = example.find(2); 480 | if (search != example.end()) { 481 | std::cout << "Found " << (*search) << '\n'; 482 | } else { 483 | std::cout << "Not found\n"; 484 | } 485 | } 486 | 487 | s.count(value); 488 | // O(log(size of set)) 489 | // count number of values in set 490 | // you can use to check value exist or not 491 | 492 | s.lower_bound(value); 493 | // O(log(size of set)) 494 | // Returns an iterator pointing to the first element that is not less than key. 495 | 496 | s.upper_bound(); 497 | // O(log(size of set)) 498 | // Returns an iterator pointing to the first element that is greater than key. 499 | 500 | s.clear(); 501 | s.size(); 502 | s.empty(); 503 | s.begin(); s.end(); s.rbegin(); s.rend(); 504 | // O(1) 505 | 506 | // print 507 | - for loop 508 | - using iterator 509 | ``` 510 | 511 | ### Multiset 512 | ``` 513 | Same as set but you can store multiple values 514 | ``` 515 | 516 | ### Unordered set 517 | ``` 518 | Not sorted (unordered) 519 | Average time comp of operations is O(1) 520 | worst case O(n) 521 | ``` 522 | 523 | ### Map 524 | ```cpp 525 | map m; 526 | 527 | m.insert({key, value}); 528 | // O(log(size of map)) 529 | 530 | m[key] = value; 531 | // O(log(size of map)) 532 | // set key to value 533 | // if not exist create key 534 | 535 | m.size(); 536 | m.empty(); 537 | m.clear(); 538 | m.begin(); m.end(); m.rbegin(); m.rend(); 539 | // O(1) 540 | 541 | m.find(key); 542 | // O(log(size of map)) 543 | // if != m.end() key exist else not exist 544 | 545 | m.count(key); 546 | // O(log(size of map)) 547 | // count keys 548 | 549 | // print 550 | for(auto [key, value] : map) { 551 | cout << key << ": " << value << endl; 552 | } 553 | for(pair p : map) { 554 | cout << p.first << ": " << p.second << endl; 555 | } 556 | for(auto it : map) { 557 | cout << (*it).first << endl; 558 | cout << (*it).second << endl; 559 | } 560 | // using iterator 561 | ``` 562 | 563 | ### Multimap 564 | ``` 565 | Same as map but you can store multiple keys 566 | operator [] not possible because we don't know which key to update (: 567 | ``` 568 | ```cpp 569 | multimap m; 570 | m[1] = "hi"; 571 | // error :| 572 | 573 | m.insert({1, "hi"}); 574 | m.insert({1, "hello"}); 575 | ``` 576 | 577 | ### Unordered map 578 | ```cpp 579 | Average time comp of operations is O(1) 580 | worst case O(n) 581 | ``` 582 | 583 | ### Pbds 584 | ```cpp 585 | // In the set we can't get value at particular index but with pbds we can (: 586 | 587 | #include 588 | using namespace std; 589 | 590 | #include 591 | #include 592 | using namespace __gnu_pbds; 593 | 594 | template > 595 | using pbds = tree; 596 | 597 | // order_of_key(k) : number of elements strictly smaller than k 598 | // find_by_order(ind) : iterator to set[ind] 599 | 600 | int main() 601 | { 602 | pbds A; 603 | 604 | //Add elements in any random order 605 | A.insert(11); 606 | A.insert(1); 607 | A.insert(5); 608 | A.insert(3); 609 | A.insert(7); 610 | A.insert(9); 611 | 612 | // Total contents 613 | for(int x : A) { 614 | cout << x << " "; 615 | } 616 | cout << "\n"; 617 | 618 | // K-th smallest 619 | int k = 3; 620 | cout << k << "rd smallest: " << *A.find_by_order(k - 1) << endl; 621 | 622 | k = 5; 623 | cout << k << "th smallest: " << *A.find_by_order(k - 1) << endl; 624 | 625 | // No of elements < X 626 | int X = 9; 627 | cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl; 628 | 629 | // Delete elements 630 | cout << "Deleted 3" << endl; 631 | A.erase(3); 632 | 633 | // Total contents 634 | for(int x : A) { 635 | cout << x << " "; 636 | } 637 | cout << "\n"; 638 | 639 | cout << "No of elements less than " << X << " are " << A.order_of_key(X) << endl; 640 | 641 | // Next Bigger / Smaller element than X 642 | X = 8; 643 | cout << "Next greater element than " << X << " is " << *A.upper_bound(X) << endl; 644 | 645 | return 0; 646 | } 647 | /* for custom comparator 648 | auto comp = [](const int &lhs, const int &rhs) { 649 | return lhs > rhs; 650 | }; 651 | pbds p(comp); 652 | */ 653 | ``` 654 | 655 | ### Bitset 656 | https://devdocs.io/cpp/utility/bitset 657 | 658 | ```cpp 659 | The class template bitset represents a fixed-size sequence of N bits. 660 | Bitsets can be manipulated by standard logic operators and converted to and from strings and integers. 661 | 662 | #define setbit(pos,n) n |= (1LL << pos) 663 | #define resetbit(pos,n) n &= (~(1LL << pos)) 664 | #define toogle(pos,n) n ^= (1LL << pos) 665 | #define atpos(pos,n) ((n & (1LL << pos)) != 0) 666 | #define bits(n) (floor(log2(n)) + 1) 667 | #define rightone(n) (__builtin_ffs(n) - 1) 668 | #define countone(n) (__builtin_popcountll(n)) 669 | #define parity(n) (__builtin_parity(n)) 670 | #define counttz(n) (__builtin_ctzll(n)) 671 | #define countlz(n) (__builtin_clzll(n)) 672 | ``` 673 | 674 | ### Lambda function 675 | ```cpp 676 | [pass by value | reference](parameters) -> return type { 677 | // code logic 678 | }; 679 | 680 | auto sum = [](int a, int b) { 681 | return a + b; 682 | }; 683 | 684 | int c = 10; 685 | auto sum = [c](int a, int b) { 686 | // you can access c here (pass by value) 687 | return a + b + c; 688 | }; 689 | auto sum = [&c](int a, int b) { 690 | // you can access c here (pass by reference) 691 | c = 1; 692 | return a + b + c; 693 | }; 694 | // c = 1 here 695 | 696 | [&] => you can access all varaible pass by reference 697 | [=] => ... pass by value 698 | 699 | int x = 10; 700 | auto add = [&](int y) { 701 | x += y; 702 | }; 703 | add(5); 704 | // x = 15 here 705 | 706 | // Note: 707 | // recursion not possible 708 | ``` 709 | 710 | ### Function 711 | ```cpp 712 | function name = lambda function; 713 | 714 | function fact = [&](int n) { 715 | if(n == 0) return 1; 716 | return n * fact(n - 1); 717 | }; 718 | cout << fact(5); 719 | 720 | // Note: 721 | // [&] needed to capture fact 722 | // recursion possible 723 | ``` 724 | 725 | ### Algorithms 726 | ```cpp 727 | lower_bound(); 728 | upper_bound(); 729 | sort(); 730 | reverse(); 731 | unique(); 732 | is_sorted(); 733 | binary_search(); 734 | min_element(); 735 | max_element(); 736 | next_permutation(); 737 | prev_permutation(); 738 | count(); 739 | rotate(); 740 | swap(x, y); 741 | ... 742 | ``` -------------------------------------------------------------------------------- /Learn/C++/custom comparator.md: -------------------------------------------------------------------------------- 1 | ## Custom Comparator in C++ 2 | 3 | 4 | ### 1. operator < 5 | ```c++ 6 | struct node 7 | { 8 | ll a, b; 9 | node(ll x, ll y) 10 | { 11 | a = x; 12 | b = y; 13 | } 14 | friend bool operator < (const node &lhs, const node &rhs) { 15 | return lhs.a > rhs.a; 16 | } 17 | /* 18 | bool operator < (const node &rhs) const { 19 | return a > rhs.a; 20 | } 21 | */ 22 | }; 23 | 24 | priority_queue pq; 25 | pq.push(node(1, 2)); 26 | pq.push(node(3, 2)); 27 | // pq = {[1,2], [3,2]} 28 | 29 | set s; 30 | s.insert(node(1, 2)); 31 | s.insert(node(3, 2)); 32 | // s = {[3,2], [1,2]} 33 | 34 | vector v; 35 | v.push_back(node(3, 2)); 36 | v.push_back(node(1, 2)); 37 | sort(v.begin(), v.end()); 38 | // v = {[3,2], [1,2]} 39 | ``` 40 | 41 |
42 | complete code 43 | 44 | ```c++ 45 | #include 46 | using namespace std; 47 | 48 | #include 49 | #include 50 | using namespace __gnu_pbds; 51 | 52 | template > 53 | using pbds = tree; 54 | 55 | using ll = long long int; 56 | #define endl '\n' 57 | 58 | struct node 59 | { 60 | ll a, b; 61 | node(ll x = 0, ll y = 0) { 62 | a = x; 63 | b = y; 64 | } 65 | friend bool operator < (const node &lhs, const node &rhs) { 66 | return lhs.a > rhs.a; 67 | } 68 | }; 69 | 70 | void solve(ll &tc) 71 | { 72 | priority_queue pq; 73 | pq.push(node(1, 2)); 74 | pq.push(node(3, 2)); 75 | 76 | set s; 77 | s.insert(node(1, 2)); 78 | s.insert(node(3, 2)); 79 | 80 | vector v; 81 | v.push_back(node(3, 2)); 82 | v.push_back(node(1, 2)); 83 | sort(v.begin(), v.end()); 84 | } 85 | 86 | int main() 87 | { 88 | ios::sync_with_stdio(0); 89 | cin.tie(0); 90 | 91 | ll tc = 1; 92 | for(ll i = 1; i <= tc; i++) { 93 | solve(i); 94 | } 95 | return 0; 96 | } 97 | ``` 98 | 99 |
100 | 101 | 102 | ### 2. lambda expression 103 | ```c++ 104 | auto comp = [](const node &lhs, const node &rhs) { 105 | return lhs.a > rhs.a; 106 | }; 107 | 108 | priority_queue, decltype(comp)> pq(comp); 109 | pq.push(node(1, 2)); 110 | pq.push(node(3, 2)); 111 | // pq = {[1,2], [3,2]} 112 | 113 | set s(comp); 114 | s.insert(node(1, 2)); 115 | s.insert(node(3, 2)); 116 | // s = {[3,2], [1,2]} 117 | 118 | map m(comp); 119 | m[node(1, 2)] = 1; 120 | m[node(1, 3)] = 5; 121 | // m = {([1,2], 5)} 122 | 123 | pbds p(comp); 124 | p.insert(3); 125 | p.insert(2); 126 | // p = {3, 2} 127 | 128 | vector v; 129 | v.push_back(node(3, 2)); 130 | v.push_back(node(1, 2)); 131 | sort(v.begin(), v.end(), comp); 132 | // v = {[3,2], [1,2]} 133 | ``` 134 | 135 |
136 | complete code 137 | 138 | ```c++ 139 | #include 140 | using namespace std; 141 | 142 | #include 143 | #include 144 | using namespace __gnu_pbds; 145 | 146 | template > 147 | using pbds = tree; 148 | 149 | using ll = long long int; 150 | #define endl '\n' 151 | 152 | struct node 153 | { 154 | ll a, b; 155 | node(ll x = 0, ll y = 0) { 156 | a = x; 157 | b = y; 158 | } 159 | }; 160 | 161 | void solve(ll &tc) 162 | { 163 | auto comp = [](const node &lhs, const node &rhs) { 164 | return lhs.a > rhs.a; 165 | }; 166 | 167 | priority_queue, decltype(comp)> pq(comp); 168 | pq.push(node(1, 2)); 169 | pq.push(node(3, 2)); 170 | 171 | set s(comp); 172 | s.insert(node(1, 2)); 173 | s.insert(node(3, 2)); 174 | 175 | map m(comp); 176 | m[node(1, 2)] = 1; 177 | m[node(1, 3)] = 5; 178 | 179 | pbds p(comp); 180 | p.insert(3); 181 | p.insert(2); 182 | 183 | vector v; 184 | v.push_back(node(3, 2)); 185 | v.push_back(node(1, 2)); 186 | sort(v.begin(), v.end(), comp); 187 | } 188 | 189 | int main() 190 | { 191 | ios::sync_with_stdio(0); 192 | cin.tie(0); 193 | 194 | ll tc = 1; 195 | for(ll i = 1; i <= tc; i++) { 196 | solve(i); 197 | } 198 | return 0; 199 | } 200 | ``` 201 |
-------------------------------------------------------------------------------- /Learn/C++/for loop.md: -------------------------------------------------------------------------------- 1 | ## one loop for everything 2 | 3 | 4 | > _Personally i don't recommend to use macro, but for speed it's your choice :octocat:_ 5 | 6 | ```c++ 7 | #define FOR(i, a, b, s) for(ll i = (a); i < (b); i += (s)) 8 | #define ROF(i, a, b, s) for(ll i = (a); i > (b); i += (s)) 9 | #define FOR1(n) FOR(_, 0, n, 1) 10 | #define FOR2(i, n) FOR(i, 0, n, 1) 11 | #define FOR3(i, a, b) FOR(i, a, b, 1) 12 | #define FOR4(i, a, b, s) FOR(i, a, b, s) 13 | #define ROF1(i, n) ROF(i, n, -1, -1) 14 | #define ROF2(i, a, b) ROF(i, a, b, -1) 15 | #define ROF3(i, a, b, s) ROF(i, a, b, s) 16 | #define GETC(a, b, c, d, e, ...) e 17 | #define FORC(...) GETC(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1) 18 | #define ROFC(...) GETC(__VA_ARGS__, ROF3, ROF2, ROF1) 19 | #define loop(...) FORC(__VA_ARGS__)(__VA_ARGS__) 20 | #define rloop(...) ROFC(__VA_ARGS__)(__VA_ARGS__) 21 | 22 | void solve(int &T) 23 | { 24 | // for(ll _ = 0; _ < 5; _++) 25 | loop(5) { 26 | cout << _ << " "; 27 | } 28 | cout << endl; 29 | 30 | // for(ll j = 0; j < 5; j++) 31 | loop(j, 5) { 32 | cout << j << " "; 33 | } 34 | cout << endl; 35 | 36 | // for(ll j = 5; j < 10; j++) 37 | loop(j, 5, 10) { 38 | cout << j << " "; 39 | } 40 | cout << endl; 41 | 42 | // for(ll j = 5; j < 10; j += 2) 43 | loop(j, 5, 10, 2) { 44 | cout << j << " "; 45 | } 46 | cout << endl; 47 | 48 | // for(ll k = 5; k > -1; k--) 49 | rloop(k, 5, -1) { 50 | cout << k << " "; 51 | } 52 | cout << endl; 53 | 54 | // for(ll k = 10; k > -1; k += (-3)) 55 | rloop(k, 10, -1, -3) { 56 | cout << k << " "; 57 | } 58 | cout << endl; 59 | 60 | // for(ll i = 10; i < 5; i++) 61 | loop(i, 10, 5) { 62 | assert(0); 63 | } 64 | 65 | // for(ll i = 10; i > 10; i--) 66 | rloop(i, 10, 10) { 67 | assert(0); 68 | } 69 | 70 | // for(ll i = 4; i < 10;) 71 | loop(i, 4, 10, 0) { 72 | cout << i << " "; 73 | i++; 74 | } 75 | cout << endl; 76 | 77 | // for(ll i = 10; i > 4;) 78 | rloop(i, 10, 4, 0) { 79 | cout << i << " "; 80 | i--; 81 | } 82 | cout << endl; 83 | 84 | // for(ll i = 10; i > -1; i--) 85 | rloop(i, 10) { 86 | cout << i << " "; 87 | } 88 | cout << endl; 89 | } 90 | ``` 91 | 92 |
93 | complete code 94 | 95 | ```c++ 96 | #include 97 | using namespace std; 98 | 99 | using ll = long long int; 100 | #define endl '\n' 101 | 102 | #define FOR(i, a, b, s) for(ll i = (a); i < (b); i += (s)) 103 | #define ROF(i, a, b, s) for(ll i = (a); i > (b); i += (s)) 104 | #define FOR1(n) FOR(_, 0, n, 1) 105 | #define FOR2(i, n) FOR(i, 0, n, 1) 106 | #define FOR3(i, a, b) FOR(i, a, b, 1) 107 | #define FOR4(i, a, b, s) FOR(i, a, b, s) 108 | #define ROF1(i, n) ROF(i, n, -1, -1) 109 | #define ROF2(i, a, b) ROF(i, a, b, -1) 110 | #define ROF3(i, a, b, s) ROF(i, a, b, s) 111 | #define GETC(a, b, c, d, e, ...) e 112 | #define FORC(...) GETC(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1) 113 | #define ROFC(...) GETC(__VA_ARGS__, ROF3, ROF2, ROF1) 114 | #define loop(...) FORC(__VA_ARGS__)(__VA_ARGS__) 115 | #define rloop(...) ROFC(__VA_ARGS__)(__VA_ARGS__) 116 | 117 | void solve(ll &tc) 118 | { 119 | // for(ll _ = 0; _ < 5; _++) 120 | loop(5) { 121 | cout << _ << " "; 122 | } 123 | cout << endl; 124 | 125 | // for(ll j = 0; j < 5; j++) 126 | loop(j, 5) { 127 | cout << j << " "; 128 | } 129 | cout << endl; 130 | 131 | // for(ll j = 5; j < 10; j++) 132 | loop(j, 5, 10) { 133 | cout << j << " "; 134 | } 135 | cout << endl; 136 | 137 | // for(ll j = 5; j < 10; j += 2) 138 | loop(j, 5, 10, 2) { 139 | cout << j << " "; 140 | } 141 | cout << endl; 142 | 143 | // for(ll k = 5; k > -1; k--) 144 | rloop(k, 5, -1) { 145 | cout << k << " "; 146 | } 147 | cout << endl; 148 | 149 | // for(ll k = 10; k > -1; k += (-3)) 150 | rloop(k, 10, -1, -3) { 151 | cout << k << " "; 152 | } 153 | cout << endl; 154 | 155 | // for(ll i = 10; i < 5; i++) 156 | loop(i, 10, 5) { 157 | assert(0); 158 | } 159 | 160 | // for(ll i = 10; i > 10; i--) 161 | rloop(i, 10, 10) { 162 | assert(0); 163 | } 164 | 165 | // for(ll i = 4; i < 10;) 166 | loop(i, 4, 10, 0) { 167 | cout << i << " "; 168 | i++; 169 | } 170 | cout << endl; 171 | 172 | // for(ll i = 10; i > 4;) 173 | rloop(i, 10, 4, 0) { 174 | cout << i << " "; 175 | i--; 176 | } 177 | cout << endl; 178 | 179 | // for(ll i = 10; i > -1; i--) 180 | rloop(i, 10) { 181 | cout << i << " "; 182 | } 183 | cout << endl; 184 | } 185 | 186 | int main() 187 | { 188 | ios::sync_with_stdio(0); 189 | cin.tie(0); 190 | 191 | ll tc = 1; 192 | for(ll i = 1; i <= tc; i++) { 193 | solve(i); 194 | } 195 | return 0; 196 | } 197 | ``` 198 |
199 | -------------------------------------------------------------------------------- /Learn/README.md: -------------------------------------------------------------------------------- 1 | # 📑 Learn 2 | 3 | ### DSA 4 | - practice and practice 5 | 6 | ### OOP in C++ 7 | - [Start with basic - w3school](https://www.w3schools.com/cpp/cpp_oop.asp) 8 | - [**Must check** this out](https://github.com/dhiraj-01/CP/blob/main/Learn/oop%20c%2B%2B.md) 9 | - [Examples](https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html) 10 | - [Oops interview questions - Interviewbit](https://www.interviewbit.com/oops-interview-questions/) 11 | - [Detail explanation - learncpp](https://www.learncpp.com/cpp-tutorial/welcome-to-object-oriented-programming/) 12 | 13 | ### SQL 14 | - https://www.w3schools.com/sql/default.asp 15 | - https://github.com/WebDevSimplified/Learn-SQL 16 | - [Code with Mosh - Youtube](https://youtu.be/7S_tz1z_5bA) 17 | 18 | ### DBMS 19 | - https://www.edureka.co/blog/interview-questions/dbms-interview-questions 20 | - https://www.softwaretestinghelp.com/top-dbms-interview-questions/ 21 | - [DBMS interview questions - Interviewbit](https://www.interviewbit.com/dbms-interview-questions/) 22 | -------------------------------------------------------------------------------- /Learn/dynamic programming.md: -------------------------------------------------------------------------------- 1 | # Dynamic Programming 2 | 3 | **Steps to solve dp problems** 4 | - Identify it's dp problem (keys: count total ways, minimize or maximize answer type problems) 5 | - Identify dimention and states 6 | - State transition 7 | - Recustion + memorization 8 | - Done :) 9 | 10 | ``` 11 | Top Down = Recursive 12 | Bottom Up = Iterative 13 | ``` 14 | 15 | ### Fibonacci Number 16 | ```cpp 17 | fib(0) = 0 18 | fib(1) = 1 19 | fib(n) = fib(n - 1) + fib(n - 2), n >= 2 20 | ``` 21 | 22 | ### Staircase problem 23 | ``` 24 | Given a distance dist, count total number of ways to cover the distance with 1, 2 and 3 steps. 25 | dp[i] = dp[i - 1] + dp[i - 2] + dp[i - 3] 26 | ``` 27 | 28 | ### Longest Common Subsequence 29 | ```cpp 30 | dp[i][j] = length of lcs of substring(0, i) of s1, substring(0, j) of s2 31 | 32 | dp[i][j] = { 33 | if(s1[i] == s2[j]) { 34 | dp[i][j] = 1 + dp[i - 1][j - 1]; 35 | } 36 | else { 37 | max(dp[i][j - 1], dp[i - 1][j]); 38 | } 39 | } 40 | 41 | answer = dp[s1.size()][s2.size()] 42 | ``` 43 | 44 | ### Longest Common Substring 45 | ```cpp 46 | dp[i][j] = length of lcs ending at i in s1, at j in s2 47 | 48 | dp[i][j] = { 49 | if(s1[i] == s2[j]) { 50 | dp[i][j] = 1 + dp[i - 1][j - 1]; 51 | } 52 | else { 53 | // s1[i] != s2[j] so length of lcs ending at i, j is 0 54 | dp[i][j] = 0; 55 | } 56 | } 57 | 58 | ans = 0; 59 | for(ll i = 0; i < n; i++) { 60 | for(ll j = 0; j < m; j++) { 61 | ans = max(ans, dp[i][j]); 62 | } 63 | } 64 | ``` 65 | 66 | ### Longest Palindromic Subsequence 67 | ```cpp 68 | - solution(1) 69 | LPS of string s = Longest Common Subsequence(s, reverse(s)) 70 | 71 | - solution(2) 72 | dp[i][j] = lps of substring(i, j) of s 73 | 74 | dp[i][j] = { 75 | if(s[i] == s[j]) { 76 | dp[i][j] = 1 + dp[i + 1][j - 1]; 77 | } 78 | else { 79 | dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]); 80 | } 81 | } 82 | answer = dp[1][s.size()] 83 | ``` 84 | 85 | ### Longest Palindromic Substring 86 | ```cpp 87 | - solution(1) 88 | consider each index as center of palindrome, expand till palindrome and maximize answer 89 | O(n ^ 2) worst case 90 | 91 | for(int c = 0; c < n; c++) 92 | { 93 | // odd length 94 | { 95 | int len = 1; 96 | int i = c - 1, j = c + 1; 97 | while(s[i] == s[j]) { 98 | i--, j++; 99 | len += 2; 100 | } 101 | } 102 | // even length 103 | { 104 | int len = 0; 105 | int i = c - 1, j = c; 106 | while(s[i] == s[j]) { 107 | i--, j++; 108 | len += 2; 109 | } 110 | } 111 | } 112 | 113 | - solution(2) 114 | https://cp-algorithms.com/string/manacher.html 115 | ``` 116 | 117 | ### Longest Repeated Subsequence 118 | ``` 119 | find longest subsequnce of string that occures at least twice. 120 | any ith character in the two subsequences shouldn’t have the same index in 121 | the original string. (The occurrences should not overlap) 122 | 123 | similar to longest common subsequence, just ignore when i = j and s[i] = s[j] 124 | modified lcs(s, s) 125 | ``` 126 | ```cpp 127 | dp[i][j] = { 128 | if(s[i] == s[j] and i != j) { 129 | dp[i][j] = 1 + dp[i - 1][j - 1]; 130 | } 131 | else { 132 | dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); 133 | } 134 | } 135 | answer = dp[s.size()][s.size()]; 136 | ``` 137 | 138 | ### Minimum edit distance 139 | ``` 140 | Given two strings str1 and str2 and below operations that can performed on str1. 141 | Find minimum number of edits (operations) required to convert str1 into str2. 142 | 143 | - Insert 144 | - Remove 145 | - Replace 146 | 147 | All of the above operations are of equal cost. 148 | ``` 149 | ```cpp 150 | dp[i][j] = min cost to convert substring(0, i) of s1 to substring(0, j) of s2 151 | 152 | dp[i][j] = { 153 | if(s1[i] == s2[j]) { 154 | dp[i][j] = dp[i - 1][j - 1]; 155 | } 156 | else { 157 | dp[i][j] = min( 158 | 1 + dp[i][j - 1], // insert s2[j] between (i, i + 1) in s1 159 | 1 + dp[i - 1][j] // delete s1[i] 160 | 1 + dp[i - 1][j - 1] // replace s1[i] = s2[j] 161 | ); 162 | } 163 | } 164 | answer = dp[s1.size()][s2.size()] 165 | ``` 166 | 167 | ### Shortest Common Supersequence 168 | ``` 169 | Given two strings str1 and str2, the task is to find the length of the shortest string that has both str1 and str2 as subsequences. 170 | ``` 171 | ```cpp 172 | - solution(1) 173 | Length of the shortest supersequence 174 | = (Sum of lengths of given two strings) - (Length of LCS of two given strings) 175 | = s1.size() + s2.size() - LCS(s1, s2) 176 | 177 | - solution(2) 178 | dp[i][j] = length of scs of substring(0, i) of s1 and substring(0, j) of s2 179 | 180 | dp[i][j] = { 181 | if(s1[i] == s1[j]) { 182 | dp[i][j] = 1 + dp[i - 1][j - 1] // insert s1[i] 183 | } 184 | else { 185 | dp[i][j] = min( 186 | 1 + dp[i - 1][j], // insert s1[i] 187 | 1 + dp[i][j - 1] // insert s2[j] 188 | ); 189 | } 190 | } 191 | answer = dp[s1.size()][s2.size()] 192 | ``` 193 | 194 | ### Longest Increasing Subsequence 195 | ``` 196 | The Longest Increasing Subsequence (LIS) problem is to find the length of the 197 | longest subsequence of a given sequence such that all elements of the subsequence 198 | are sorted in increasing order. For example, the length of LIS for 199 | {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}. 200 | ``` 201 | ```cpp 202 | - solution(1) O(n ^ 2) 203 | dp[i] = lis ending at index i 204 | 205 | dp[1] = 1; 206 | for(ll i = 2; i <= n; i++) { 207 | for(ll j = 1; j < i; j++) { 208 | if(a[j] < a[i]) { 209 | dp[i] = max(dp[i], 1 + dp[j]); 210 | } 211 | } 212 | } 213 | 214 | ans = 0; 215 | for(ll i = 1; i <= n; i++) { 216 | ans = max(ans, dp[i]); 217 | } 218 | 219 | - solution(2) O(n * logn) 220 | dp[i] = min value for lis of length i 221 | ex. ar = {5, 2, 1, 4} 222 | dp[1] = {1}, 223 | dp[2] = {4} 224 | 225 | ll lengthOfLIS(vector &a) 226 | { 227 | const ll INF = 1e9; 228 | ll n = a.size(); 229 | 230 | // dp[i] = minimum last element for LIS of length i 231 | vector dp(n + 1, INF); 232 | dp[0] = -INF; 233 | 234 | for(ll i = 0; i < n; i++) 235 | { 236 | ll id = upper_bound(dp.begin(), dp.begin() + i + 1, a[i]) - dp.begin(); 237 | // if(dp[id - 1] <= a[i]) { // LIS 238 | if(dp[id - 1] < a[i]) { // LSIS 239 | dp[id] = a[i]; 240 | } 241 | } 242 | for(ll len = n; len >= 1; len--) { 243 | if(dp[len] < INF) { 244 | return len; 245 | } 246 | } 247 | return 0; 248 | } 249 | ``` 250 | 251 | ### Longest Bitonic Subsequence 252 | ``` 253 | a subsequence of array is called Bitonic if it is first increasing, then decreasing 254 | 255 | answer is S[i] + E[i] - 1 256 | S[i] = longest increasing subsequence starting from i 257 | E[i] = longest increasing subsequence ending at i 258 | ``` 259 | 260 | ### Maximum Sum Increasing Subsequence 261 | ```cpp 262 | For example, if input is {1, 101, 2, 3, 100, 4, 5}, then output should be 106 (1 + 2 + 3 + 100) 263 | 264 | dp[i] = maximum sum of incresing subsequence ending at index i 265 | 266 | dp[1] = a[1]; 267 | for(ll i = 2; i <= n; i++) { 268 | dp[i] = a[i]; 269 | for(ll j = 1; j < i; j++) { 270 | if(a[j] < a[i]) { 271 | dp[i] = max(dp[i], a[i] + dp[j]); 272 | } 273 | } 274 | } 275 | ``` 276 | 277 | ### Find the size of the largest square submatrix of 1’s present in a binary matrix 278 | ```cpp 279 | [ 280 | [0 0 1 0 1 1] 281 | [0 1 1 1 0 0] 282 | [0 0 1 1 1 1] 283 | [1 1 0 1 1 1] 284 | [1 1 1 1 1 1] 285 | [1 1 0 1 1 1] 286 | [1 0 1 1 1 1] 287 | [1 1 1 0 1 1] 288 | ] 289 | answer = 3 290 | 291 | dp[i][j] = largest square matrix ending at cell(i, j) 292 | 293 | dp[i][j] = { 294 | if(a[i][j] == 0) { 295 | dp[i][j] = 0; 296 | } 297 | else { 298 | dp[i][j] = 1 + min( 299 | dp[i - 1][j - 1], 300 | dp[i][j - 1], 301 | dp[i - 1][j] 302 | ); 303 | } 304 | } 305 | 306 | answer = max in dp 307 | ``` 308 | 309 | ### Matrix Chain Multiplication 310 | ``` 311 | Given a sequence of matrices, find the most efficient way to multiply these 312 | matrices together. The problem is not actually to perform the multiplications, 313 | but merely to decide in which order to perform the multiplications. 314 | 315 | For example, suppose A is a 10 × 30 matrix, B is a 30 × 5 matrix, and C is a 316 | 5 × 60 matrix. Then, 317 | 318 | (AB)C = (10 × 30 × 5) + (10 × 5 × 60) = 1500 + 3000 = 4500 operations 319 | A(BC) = (30 × 5 × 60) + (10 × 30 × 60) = 9000 + 18000 = 27000 operations. 320 | ``` 321 | ```cpp 322 | total matrix n 323 | given array a of size n + 1 324 | dimention of matrix i = a[i] x a[i + 1] 325 | 326 | dp[i][j] = minimum multiplications to multiply matrix i to j 327 | 328 | for(ll k = i; k < j; k++) { 329 | // multiply i to k, k + 1 to j 330 | // then multiply resulting matrix (a[i] * a[k + 1]) x (a[k + 1] * a[j]) 331 | dp[i][j] = dp[i][k] + dp[k + 1][j] + (a[i] * a[k + 1] * a[j]); 332 | } 333 | answer = dp[1][n] 334 | ``` 335 | 336 | ### Find total ways to reach the last cell of a matrix from its first cell 337 | ``` 338 | dp[i][j] = total ways to reach cell(i, j) 339 | dp[i][j] += dp[x][y], (x, y => direction) 340 | ``` 341 | 342 | ### Find minimum cost to reach the last cell of a matrix from its first cell 343 | ```cpp 344 | dp[i][j] = min cost to reach cell(i, j) 345 | dp[i][j] = cost[i][j] + min(dp[x][y]), (x, y => direction) 346 | ``` 347 | 348 | ### Count the number of paths in a matrix with a given cost to reach the destination cell 349 | ``` 350 | given cost, count total paths 351 | dp[i][j][k] = total ways to reach cell(i, j) with cost k 352 | dp[i][j][k] += dp[x][y][k - cost[i][j]], (x, y => direction) 353 | ``` 354 | 355 | ### Find the longest sequence formed by adjacent numbers in the matrix 356 | ``` 357 | dp[i][j] = longest sequence ending at cell (i, j) 358 | 359 | dp[i][j] = 1 + dp[x][y], if(a[x][y] == a[i][j] - 1), (x, y => direction) 360 | (x, y) = {(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)} 361 | ``` 362 | 363 | ### 01 - Knapsack 364 | ### Fractional knapsack 365 | ### Unbounded knapsack 366 | ### Coin Change 367 | 368 | ### Subset Sum 369 | - exist subset with given sum 370 | - count total subset with given sum 371 | - equal sum partition 372 | - mimimum sum partition 373 | 374 | ### Rod cutting 375 | ```cpp 376 | dp[i] = max value by cutting rod of length i 377 | 378 | for(ll j = 1; j <= n; j++) { 379 | if(i >= j) { 380 | dp[i] = max(dp[i], price[j] + dp[i - j]); 381 | } 382 | } 383 | ``` 384 | 385 | ### Maximum Product Rod Cutting 386 | ```cpp 387 | // find max product 388 | ex. n = 4, 389 | 1 * 1 * 1 * 1 = 1 390 | 2 * 2 = 4 391 | 1 * 3 = 3 392 | 393 | dp[i] = i; 394 | for(ll j = 1; j < i; j++) { 395 | dp[i] = max(dp[i], j * dp[i - j]); 396 | } 397 | ``` 398 | 399 | ### Deque Game 400 | https://atcoder.jp/contests/dp/tasks/dp_l 401 | ``` 402 | dp[i][j] = optimal answer = maximum diff (x - y) 403 | x = first player score 404 | y = second player score 405 | 406 | dp[i][j] = max( 407 | a[i] - dp[i + 1][j], 408 | a[j] - dp[i][j - 1] 409 | ); 410 | ``` 411 | https://codeforces.com/contest/859/problem/C 412 | https://atcoder.jp/contests/abc201/tasks/abc201_d 413 | 414 | ### Jump Game 415 | 416 | ### Find all N-digit binary strings without any consecutive 1's 417 | ``` 418 | Input: N = 2 419 | Output: 3 420 | The 3 strings are 00, 01, 10 421 | 422 | Input: N = 3 423 | Output: 5 424 | The 5 strings are 000, 001, 010, 100, 101 425 | ``` 426 | ```cpp 427 | dp[i][j] = total ith digit strings, ending at char j 428 | 429 | dp[i][j] = { 430 | if(j == '0') { 431 | dp[i][j] = dp[i - 1]['0'] + dp[i - 1]['1']; 432 | } 433 | else { 434 | dp[i][j] = dp[i - 1]['0']; 435 | } 436 | } 437 | ``` 438 | -------------------------------------------------------------------------------- /Problems/2 pointer/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an array of size n and k 4 | Return the count of subsequences "subseq" where max(subseq) - min(subseq) <= k 5 | 1 <= n <= 1e5 6 | ``` 7 | 8 | ### Solution 9 | ``` 10 | sort array + 2 pointer 11 | for each l = 0 to l = n 12 | current index l, find max r such that (a[r] - a[l]) <= k 13 | ans += 2 ^ (r - l) 14 | ``` 15 | similar https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/ 16 | 17 | ### Bonus 18 | ``` 19 | how to count all subarray ? 20 | such that max(subarray) - min(subarray) <= k 21 | 22 | similar approach because minimum and maximum is monotonic function 23 | so a[l....r] is valid subarray then a[l], a[l, l+1], a[l, l+1, l+2], ... a[l, ... , r-1] is valid subarray 24 | 25 | for each l = 0 to l = n 26 | current index l, find max r such that (max in a[l...r] - min in a[l...r]) <= k 27 | ans += (r - l) 28 | ``` 29 | https://codeforces.com/edu/course/2/lesson/9/2/practice/contest/307093/problem/F 30 | 31 | -------------------------------------------------------------------------------- /Problems/2 pointer/readme.md: -------------------------------------------------------------------------------- 1 | ### 2 pointer 2 | 3 | **problems** 4 | - https://codeforces.com/edu/course/2 5 | - https://codeforces.com/contest/1475/problem/D 6 | - https://codeforces.com/gym/102942/problem/F 7 | - https://www.hackerrank.com/contests/daiict-winter-long-2021/challenges/new-year-gifts- 8 | - https://codeforces.com/edu/course/2/lesson/9/3/practice/contest/307094/problem/H 9 | - https://codeforces.com/contest/1237/problem/B 10 | - https://www.spoj.com/problems/KOIREP/ -------------------------------------------------------------------------------- /Problems/README.md: -------------------------------------------------------------------------------- 1 | ### Online Assessment & CP type problems -------------------------------------------------------------------------------- /Problems/array/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an array, and k you can select element from front or back of the array and remove that element, 4 | you have to select K element from element such that their sum is maximum. 5 | 6 | Ex. [2, 1, 3, 10], k = 2 7 | Ans = 13 8 | 9 | Ex. [1, 100, 2, 3], K = 2 10 | Ans = 101 11 | ``` 12 | https://leetcode.com/problems/maximum-points-you-can-obtain-from-cards/ 13 | 14 | ### Solution 15 | ``` 16 | 3 possible case 17 | Take all element from front of the array 18 | Take all element from back of the array 19 | Take x element from front and y element from back such that x + y = k 20 | 21 | Create prefix sum and suffix sum array 22 | ``` 23 | 24 |
25 | code in c++ 26 | 27 | ```cpp 28 | int solve(vector arr, int k) 29 | { 30 | int n = arr.size(); 31 | vector suffix(n + 1, 0); 32 | for(int i = n - 1; i >= 0; i--) { 33 | suffix[i] += suffix[i + 1]; 34 | suffix[i] += arr[i]; 35 | } 36 | 37 | int ans = 0; 38 | int prefix = 0; 39 | for(int i = 0; i < k; i++) 40 | { 41 | ans = max(ans, prefix + suffix[n - (k - i)]); 42 | prefix += arr[i]; 43 | } 44 | ans = max(ans, prefix); 45 | return ans; 46 | } 47 | ``` 48 |
49 | -------------------------------------------------------------------------------- /Problems/bitmask dp/readme.md: -------------------------------------------------------------------------------- 1 | ### Bitmask dp 2 | 3 | **problems** 4 | - https://atcoder.jp/contests/abc180/tasks/abc180_e 5 | - https://atcoder.jp/contests/abc190/tasks/abc190_e 6 | - https://codeforces.com/contest/16/problem/E 7 | - https://www.codechef.com/problems/TSHIRTS 8 | - https://atcoder.jp/contests/abc142/tasks/abc142_e 9 | - https://codeforces.com/problemset/problem/8/C 10 | - https://www.hackerrank.com/contests/daiict-ipc-6-2021/challenges/max-mex-mex/problems 11 | 12 | **problems | string** 13 | - https://leetcode.com/problems/find-the-longest-substring-containing-vowels-in-even-counts/ 14 | - https://leetcode.com/problems/find-longest-awesome-substring/ 15 | - https://leetcode.com/problems/number-of-wonderful-substrings/ -------------------------------------------------------------------------------- /Problems/dynamic programming/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | find number of count such that i < j < k < l < m and A[i] < A[j] > A[k] < A[l] > A[m] array is a permutation. 4 | ``` 5 | 6 | ### Solution 7 | ```cpp 8 | // dp[j] = count all pair (i, j) such that a[i] < a[j] 9 | for(int j = 0; j < n; j++) { 10 | dp[j] = 0; 11 | for(int i = 0; i < j; i++) { 12 | if(a[i] < a[j]) { 13 | dp[j]++; 14 | } 15 | } 16 | } 17 | 18 | // dp1[k] = count all pair (i, j, k) such that a[i] < a[j] > a[k] 19 | // to find all pair a[i] < a[j] we can use above dp 20 | for(int k = 0; k < n; k++) { 21 | dp1[k] = 0; 22 | for(int j = 0; j < k; j++) { 23 | if(a[j] > a[k]) { 24 | dp1[k] += dp[j]; 25 | // dp1[j] = total pair a[i] < a[j] 26 | } 27 | } 28 | } 29 | 30 | // dp2[l] = count all pair (i, j, k, l) such that a[i] < a[j] > a[k] < a[l] 31 | // to find all pair a[i] < a[j] > a[k] we can use dp1 32 | for(int l = 0; l < n; l++) { 33 | dp2[l] = 0; 34 | for(int k = 0; k < l; k++) { 35 | if(a[k] < a[l]) { 36 | dp2[l] += dp1[k]; 37 | } 38 | } 39 | } 40 | 41 | // and one more time :) 42 | // dp3[m] = count all pair (i, j, k, l, m) such that a[i] < a[j] > a[k] < a[l] > a[m] 43 | // to find all pair a[i] < a[j] > a[k] < a[l] we can use dp2 44 | for(int m = 0; m < n; m++) { 45 | dp3[m] = 0; 46 | for(int l = 0; l < m; l++) { 47 | if(a[l] > a[m]) { 48 | dp3[m] += dp2[l]; 49 | } 50 | } 51 | } 52 | 53 | // answer is sum of all dp3[i], 0 <= i < n 54 | ans = 0; 55 | for(int i = 0; i < n; i++) { 56 | ans += dp3[i]; 57 | } 58 | 59 | // time comp. O(n ^ 2), we can reduce time comp. to O(nlogn) using fenwick tree 60 | // how ? 61 | ``` 62 | similar problems 63 | - https://codeforces.com/problemset/problem/987/C 64 | - https://codeforces.com/contest/597/problem/C 65 | 66 | 67 | ### Bonus 68 | ``` 69 | count all pair i < j < k < l < m such that A[i] < A[k] < A[j] < A[m] < A[l] 70 | ``` 71 | -------------------------------------------------------------------------------- /Problems/dynamic programming/2.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | given array of n elements and target integer. 4 | We can either add the current element or substract it from result. 5 | can we find any possible combination such that result of that combination is k. 6 | 7 | ex. a = [1, 2, 3], target = 6 8 | answer : true 9 | 10 | a = [1, 2, 3], target = 2 11 | answer = true 12 | 13 | a = [1, 2, 3], target = 1 14 | answer = false 15 | 16 | possible combinations, 17 | +1 + 2 + 3 = 6 18 | +1 + 2 - 3 = 0 19 | +1 - 2 + 3 = 2 20 | +1 - 2 - 3 = -4 21 | -1 + 2 + 3 = 4 22 | -1 + 2 - 3 = -2 23 | -1 - 2 + 3 = 0 24 | -1 - 2 - 3 = -6 25 | ``` 26 | **Myntra interview question** 27 | 28 | ### Solution 29 | ``` 30 | Hint: subset sum diffrence with given target, dp 31 | ``` 32 | https://youtu.be/ot_XBHyqpFc (hindi) -------------------------------------------------------------------------------- /Problems/dynamic programming/3_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | given array of N length and variable k. 4 | Count the minimum number of elements need to change for make all k size subarrays xor is 0. 5 | 1 <= N <= 1e4 6 | 0 <= A[i] <= 1024 7 | 8 | ex. 9 | 4 10 | 1 2 3 1 11 | 3 12 | ans : 0 13 | 14 | 4 15 | 1 2 3 1 16 | 2 17 | Ans : 2 18 | ``` 19 | 20 | ### Solution 21 | 22 | Observation 1 23 | ``` 24 | N < 1e4, A[i] <= 1024 25 | so expected time / space = O(N * 1024) 26 | ``` 27 | Observations 2 28 | ``` 29 | all a[i % k] elements are same 30 | a[i] = a[i + k] = a[i + 2 * k] ... 31 | 32 | After this solutions is very straight forward using n * 1024 dp with 1024 extra iterations for each step. 33 | dp[id][curXor] = minimum number of changes required in 0 to id where curXor of 0 to id = curXor 34 | time comp. = O(k * 1024 * 1024) 35 | ending conditions => curXor == 0 && id == k 36 | ``` 37 | Observation 3 38 | ``` 39 | here, we can optimise 1024 iterations per step.. 40 | here, for some id i we need to iterate only j such that j % k = i. 41 | and we will skip 1 elements as black after iteration for subarray we will put this 42 | elements as remaining xor. it will gives always optimal answer. 43 | 44 | dp state dp[id][curXor][skip or not (0 / 1)]. 45 | time comp. = O(n * 1024 * 2) 46 | ``` 47 | 48 | ### Bonus 49 | easy version https://www.codechef.com/COOK129B/problems/CSUBS 50 | -------------------------------------------------------------------------------- /Problems/dynamic programming/4.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | There are 3 types of queries 4 | 5 | 0 X add a number X 6 | 1 X remove the number X (X always exist) 7 | 2 X return the number of subsets that sum to X 8 | 9 | 0 <= X <= 10^3 10 | 0 <= number of queries <= 10^3 11 | ``` 12 | **asked in google 2021** 13 | 14 | ### Solution 15 | ``` 16 | Hint: knapsack varient 17 | ``` 18 | ```cpp 19 | vector dp(1001, 0); 20 | dp[0] = 1; 21 | const int mod = 1e9 + 7; 22 | int Q; 23 | cin >> Q; 24 | while(Q--) { 25 | int tt, x; 26 | cin >> tt >> x; 27 | if(tt == 0) { 28 | for(int i = 1000; i >= x; --i) 29 | dp[i] = (dp[i] + dp[i - x]) % mod; 30 | } 31 | else if(tt == 1) { 32 | for(int i = x; i <= 1000; ++i) 33 | dp[i] = (dp[i] - dp[i - x] + mod) % mod; 34 | } 35 | else { 36 | cout << dp[x] << "\n"; 37 | } 38 | } 39 | ``` 40 | more discussion: https://codeforces.com/blog/entry/92845 -------------------------------------------------------------------------------- /Problems/dynamic programming/5.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an positive integer X and string S, where '0' <= S[i] <= '9' 4 | Calculate the minimum number of deletions required in S, to make it divisible. 5 | int(S) % X == 0 6 | 7 | Constraints: 8 | len(S) <= 10000, X <= 100 9 | 10 | Input: 11 | S = "123456", 12 | X = 13 13 | 14 | Output: 15 | 2 16 | 17 | Explanation: 18 | By deleting 4 and 6 from the string 19 | S = "1235" 20 | Now S % X = 0 21 | So ans in this case is 2 22 | ``` 23 | **asked in dunzo** 24 | 25 | ### Solution 26 | ``` 27 | Hint: knapsack varient ? 28 | at each index two choice either keep it or delete it 29 | ``` 30 | 31 |
32 | code c++ 33 | 34 | ```cpp 35 | ll x; 36 | string s; 37 | ll dp[10000][100]; 38 | 39 | ll f(ll index, ll mod) 40 | { 41 | if(index >= s.size()) { 42 | return mod == 0 ? 0 : 1e9; 43 | } 44 | ll& ans = dp[index][mod]; 45 | if(ans != -1) return ans; 46 | ans = min( 47 | 1 + f(index + 1, mod), 48 | f(index + 1, (mod * 10 + (s[index] - '0')) % x) 49 | ); 50 | return ans; 51 | } 52 | 53 | void solve() 54 | { 55 | x = 13; 56 | s = "123456"; 57 | memset(dp, -1, sizeof(dp)); 58 | cout << f(0, 0) << endl; 59 | } 60 | ``` 61 |
-------------------------------------------------------------------------------- /Problems/dynamic programming/readme.md: -------------------------------------------------------------------------------- 1 | ### Dynamic Programming 2 | 3 | **problems** 4 | - https://codeforces.com/gym/102942/problem/E 5 | - https://codeforces.com/problemset/problem/1051/D 6 | - https://codeforces.com/contest/909/problem/C 7 | - https://atcoder.jp/contests/abc200/tasks/abc200_d 8 | - https://atcoder.jp/contests/abc201/tasks/abc201_d 9 | - https://leetcode.com/contest/weekly-contest-241/problems/number-of-ways-to-rearrange-sticks-with-k-sticks-visible/ 10 | - https://codeforces.com/contest/1525/problem/D 11 | - https://www.codechef.com/problems/HMWPRB -------------------------------------------------------------------------------- /Problems/fenwick tree/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given array a and b of length n. 4 | optimize below code to find answer. 5 | 6 | int ans = 0; 7 | for(int i = 0; i < n; i++) { 8 | for(int j = 0; j < n; j++) { 9 | ans += max(a[i] + b[j], a[j] + b[i]); 10 | } 11 | } 12 | ans = ans % 1000000007; 13 | ``` 14 | 15 | **Constraints** 16 | ``` 17 | 1 <= n <= 1e5 18 | 1 <= a[i], b[i] <= 1e5 19 | ``` 20 | 21 | **Input** 22 | ``` 23 | a = [1, 2] 24 | b = [2, 3] 25 | ``` 26 | 27 | **Output** 28 | ``` 29 | 16 30 | ``` 31 | 32 | **Explanation** 33 | ``` 34 | ans = max(3, 3) + max(4, 4) + max(4, 4) + max(5, 5) = 16 % 1000000007 = 16 35 | ``` 36 | 37 | ### Tag 38 | `medium`, `code-nation` 39 | 40 | ### Solution 41 | ``` 42 | just focus on max(a[i] + b[j], a[j] + b[i]) 43 | 44 | { 45 | add a[i] + b[j] if? 46 | a[i] + b[j] > a[j] + b[i] 47 | a[i] - b[i] > a[j] - b[j] 48 | d[i] > d[j] (let say d[i] = a[i] - b[i]) 49 | 50 | so for all d[j] < d[i] we add a[i] + b[j] to answer 51 | } 52 | { 53 | add b[i] + a[j] if? 54 | a[j] + b[i] > a[i] + b[j] 55 | a[j] - b[j] > a[i] - b[i] 56 | d[j] > d[i] 57 | 58 | so for all d[i] < d[j] we add b[i] + a[j] to answer 59 | } 60 | { 61 | if both are equal ? 62 | d[i] = d[j] 63 | add a[i] + b[j] or b[i] + a[j] to answer 64 | } 65 | ``` 66 | 67 |
68 | code (c++) 69 | 70 | ``` 71 | your code here 72 | ``` 73 |
-------------------------------------------------------------------------------- /Problems/fenwick tree/readme.md: -------------------------------------------------------------------------------- 1 | ### Fenwick tree 2 | 3 | **problems** 4 | - https://www.codechef.com/problems/SPREAD 5 | - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/shil-and-palindrome-research/ 6 | - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/help-ashu-1/description/ 7 | - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/string-query-22/description/ 8 | - https://codeforces.com/contest/1234/problem/D 9 | - https://www.spoj.com/problems/INVCNT/ 10 | - https://www.spoj.com/problems/YODANESS/ 11 | - https://www.spoj.com/problems/DCEPC206/ 12 | - https://www.spoj.com/problems/MCHAOS/ 13 | - https://codeforces.com/contest/1354/problem/D 14 | - https://codeforces.com/contest/61/problem/E 15 | - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/password-cracker-de8d54f1/ 16 | - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/shil-and-round-numbers/ 17 | - https://codeforces.com/contest/459/problem/D 18 | - https://cses.fi/problemset/task/1652 19 | - https://www.spoj.com/problems/MATSUM/ 20 | - https://www.codechef.com/problems/SEACO 21 | - https://www.hackerearth.com/practice/data-structures/advanced-data-structures/fenwick-binary-indexed-trees/practice-problems/algorithm/permutation-and-inversions-43b5147e/description/ 22 | - https://atcoder.jp/contests/abc190/tasks/abc190_f 23 | - https://codeforces.com/contest/1430/problem/E 24 | - https://codeforces.com/contest/159/problem/C 25 | - https://codeforces.com/contest/1367/problem/C -------------------------------------------------------------------------------- /Problems/graph tree/1_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given a tree with N nodes and N-1 edges. Each edge has some weight(W). 4 | For every pair of vertices i and j (i != j) there exists a unique path. 5 | 6 | A special prime factor of any given path is the largest prime factor of all the edges in the path. 7 | Find the sum of special prime factors of all the possible tree paths. 8 | 9 | In other word, special prime factor of the path a to b = max prime factor is 10 | (weight of edge (a-c), weight of edge(c-d), ... , x-b (consider all weights which are in given path) 11 | 12 | Sample Input: 13 | N = 6 (Total nodes) 14 | 1 2 5 (Edge between node 1 and 2 having weight 5) 15 | 2 3 10 16 | 2 4 21 17 | 1 5 15 18 | 5 6 26 19 | 20 | Sample Output: 21 | 246 22 | 23 | Explanations: 24 | 25 | special prime factor of the path (1,2) = 5 26 | special prime factor of the path (1,3) = {(1->2) and (2->3)} = {5,10} = {5,2} = 5 (max prime factor from all the edges which are in the path) 27 | special prime factor of the path (1,4) = {(1->2) and (2->4)} = {5,21} = {5,3,7} = 7 28 | special prime factor of the path (1,5) = {(1->5)} = {15} = {3,5} = 5 29 | special prime factor of the path (1,6) = {(1->5) and (5->6)} = {15,26} = {3,5,2,13} = 13 30 | special prime factor of the path (2,1) = 5 31 | special prime factor of the path (2,3) = 5 32 | special prime factor of the path (2,4) = 7 33 | special prime factor of the path (2,5) = 5 34 | special prime factor of the path (2,6) = 13 35 | and so on for all pairs of vertices i and j , where (i!=j). 36 | 37 | Constraint: 38 | 2 <= N <= 100000 39 | 2 <= weight <= 1000000 40 | ``` 41 | 42 | ### Solution 43 | ``` 44 | :( 45 | ``` -------------------------------------------------------------------------------- /Problems/graph tree/2.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given 2 nodes in an incomplete tree on same level return 4 | the maximum nodes that can be between them if the tree is complete. 5 | ``` 6 | ![tree image](https://www.interviewcake.com/images/svgs/binary_search_tree__preview.svg?bust=209) 7 | 8 | ### Solution 9 | ``` 10 | Root = 1 11 | 12 | root = x 13 | Left child = 2 * x 14 | right child = 2 *x + 1 15 | 16 | find value for given 2 nodes 17 | answer = abs(value[node1] - value[node2]) - 1 18 | ``` -------------------------------------------------------------------------------- /Problems/graph tree/3_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | In 2D infinite grid, n Obstrangle available. 4 | we can not visit obstrangle. 5 | find the minimum path for p1(x1, y1) to p2(x2, y2). 6 | from any point (x, y) you can go any horizontal or vertical direction. 7 | ``` 8 | 9 | ### Solution 10 | ``` 11 | :( 12 | ``` -------------------------------------------------------------------------------- /Problems/graph tree/4.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | You are given a rooted binary tree with weights on edges. 4 | The distance between two nodes in this tree is the sum of the edge weights on 5 | the path between the two nodes. SUM[u] is the sum of the distances between 6 | u and all the other nodes in the tree. 7 | 8 | task1 : Calculate Sum[root] 9 | task2 : Calculate the array SUM for all nodes 1 to N. 10 | 11 | Example : 12 | Input 13 | n = total nodes 14 | (u v w) edge u to v with weight w 15 | 16 | 9 17 | 1 2 50 18 | 1 3 10 19 | 2 6 10 20 | 2 7 10 21 | 7 8 3 22 | 7 9 4 23 | 3 4 30 24 | 3 5 20 25 | 26 | Output 27 | Sum[1] = 377 28 | Sum[2] = 327 29 | Sum[3] = 407 30 | Sum[4] = 617 31 | Sum[5] = 547 32 | Sum[6] = 397 33 | Sum[7] = 357 34 | Sum[8] = 378 35 | Sum[9] = 385 36 | 37 | Expected time complexity = O(N). 38 | N = Number of node in given tree. 39 | ``` 40 | 41 | easy version https://cses.fi/problemset/task/1133/ 42 | 43 | ### Solution 44 | ``` 45 | first try to calculate answer for root then we can easily calculate answer for it childs 46 | sum[root] = sum[1] = find contribution for each edge = each edge (u, v) * subtree size of v (u is parent of v) 47 | ``` 48 | 49 |
50 | Code c++ 51 | 52 | ```cpp 53 | ll n, m; 54 | using edge = pair; 55 | vector> g; 56 | vector subtree_sz; 57 | vector ans; 58 | 59 | void clearAll(ll nn) 60 | { 61 | nn += 1; 62 | g.assign(nn, vector()); 63 | subtree_sz.assign(nn, 0); 64 | ans.assign(nn, 0); 65 | } 66 | 67 | void dfs1(ll u, ll p) 68 | { 69 | subtree_sz[u] = 1; 70 | for(auto [v, w] : g[u]) 71 | { 72 | if(v != p) 73 | { 74 | dfs1(v, u); 75 | ans[1] += w * subtree_sz[v]; 76 | subtree_sz[u] += subtree_sz[v]; 77 | } 78 | } 79 | } 80 | 81 | void dfs2(ll u, ll p) 82 | { 83 | for(auto [v, w] : g[u]) 84 | { 85 | if(v != p) 86 | { 87 | ans[v] = ans[u] - w * subtree_sz[v] + w * (n - subtree_sz[v]); 88 | dfs2(v, u); 89 | } 90 | } 91 | } 92 | 93 | void solve(ll &tc) 94 | { 95 | cin >> n; 96 | clearAll(n); 97 | 98 | m = n - 1; 99 | for(ll i = 0; i < m; i++) 100 | { 101 | ll u, v, w; 102 | cin >> u >> v >> w; 103 | g[u].emplace_back(v, w); 104 | g[v].emplace_back(u, w); 105 | } 106 | 107 | dfs1(1, 0); 108 | dfs2(1, 0); 109 | for(ll i = 1; i <= n; i++) { 110 | cout << ans[i] << " "; 111 | } 112 | cout << "\n"; 113 | } 114 | ``` 115 |
116 | -------------------------------------------------------------------------------- /Problems/graph tree/5_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | A tournament was played and score of all players is reported in a sequence, 4 | considering 2^n players. comparison is made between 2 consicutive scores on 5 | every level, highest one is declared as winner among 2. This keeps iterating 6 | until we get the final winner of tournament. 7 | 8 | Example : 9 | initial array : 10, 76, 12, 13, 45, 46, 80, 64 10 | level 1 : 76, 13, 46, 80 11 | level 2 : 76, 80 12 | level 3 : 80 13 | 14 | We will get tree below. 15 | 16 | 80 (level 3) 17 | / \ 18 | / \ 19 | / \ 20 | 76 80 (level 2) 21 | / \ / \ 22 | 76 13 46 80 (level 1) 23 | / \ / \ / \ / \ 24 | 10 76 12 13 45 46 80 64 (level 0) 25 | 26 | tree node structure : 27 | 28 | Node { 29 | int val; 30 | Node *right, *left; 31 | } 32 | 33 | You are given root of the tree. and K. 34 | return K-th highest element from the tree. 35 | ``` 36 | **asked in Disney+ Hotstar** 37 | 38 | ### Solution 39 | ``` 40 | :( 41 | ``` -------------------------------------------------------------------------------- /Problems/graph tree/6.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | You are given a binary tree with colored edges, where each edge can be either red or black. 4 | You can perform only the following operation on the tree: select a node. 5 | When you select a node, all the edges on the path from the root node to the selected node become red. 6 | Return the minimum number of operations required to color all the edges red. 7 | 8 | Example binary tree: 9 | 3 10 | / \ 11 | 1 2 12 | / \ / \ 13 | 4 6 7 10 14 | 15 | Edge colors: 16 | 3 -> 1 (black) 17 | 1 -> 4 (red) 18 | 1 -> 6 (black) 19 | 3 -> 2 (red) 20 | 2 -> 7 (black) 21 | 2 -> 10 (red) 22 | 23 | Answer is 2. 24 | 3 -> 6 and 3 -> 7 25 | ``` 26 | 27 | ### Solution 28 | ```cpp 29 | // adj[u] = [left child, right child] 30 | ll dfs(ll u) 31 | { 32 | if(u is leaf) return 0; 33 | 34 | ll v1 = adj[u][0]; // left child 35 | ll v2 = adj[u][1]; // right child 36 | 37 | ll left_ans = dfs(v1); 38 | ll right_ans = dfs(v2); 39 | 40 | ll ans = left_ans + right_ans; 41 | if(clr[u][v1] == "black" and left_ans == 0) { 42 | ans++; 43 | } 44 | if(clr[u][v2] == "black" and right_ans == 0) { 45 | ans++; 46 | } 47 | return ans; 48 | } 49 | ``` -------------------------------------------------------------------------------- /Problems/graph tree/7.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | You have a tree of N nodes, and you can do M queries on the tree. 4 | The queries are of two types - 5 | 1 a x => add a to node x and all its descendents 6 | 2 x => find the sum of values of all nodes in the path b/w the root node and the node x (both root and x inclusive). 7 | ``` 8 | 9 | ### Solution 10 | ``` 11 | Learn tree, flatten tree, segment tree :) 12 | try https://cses.fi/problemset/ tree section 13 | ``` -------------------------------------------------------------------------------- /Problems/graph tree/readme.md: -------------------------------------------------------------------------------- 1 | ### Graph Tree 2 | 3 | **problems | dijkstra** 4 | - https://codeforces.com/problemset/problem/20/C 5 | - https://codeforces.com/contest/449/problem/B -------------------------------------------------------------------------------- /Problems/greedy/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given a string of N length consisting of only lower case latin characters. 4 | Find lexicographically smallest subsequence of length exactly equal to X of this string. 5 | 6 | constraints: 7 | 1 <= N <= 1e6 8 | 1 <= X <= N 9 | 'a' <= s[i] <= 'z' 10 | 11 | input: 12 | N = 7, X = 5 13 | String = "tourist" 14 | 15 | Output: 16 | orist 17 | 18 | Among all other subsequence of length x = 5, this "orist" subsequence is the smallest of them all. 19 | ``` 20 | **asked in dunzo** 21 | 22 | ### Solution 23 |
24 | code (1) c++ 25 | 26 | ```cpp 27 | // Time: O(len * 26) 28 | // Space: O(n * 26) 29 | // too much space :( 30 | 31 | string f(string s, ll len) 32 | { 33 | ll n = s.size(); 34 | vector> pos(n, vector(26, n)); 35 | pos[n - 1][s[n - 1] - 'a'] = n - 1; 36 | for(ll i = n - 2; i >= 0; i--) { 37 | for(ll j = 0; j < 26; j++) { 38 | pos[i][j] = pos[i + 1][j]; 39 | } 40 | pos[i][s[i] - 'a'] = i; 41 | } 42 | 43 | ll last_i = 0; 44 | string ans = ""; 45 | for(ll i = len; i >= 1; i--) 46 | { 47 | bool ok = 0; 48 | for(ll j = 0; j < 26; j++) 49 | { 50 | ll x = n - pos[last_i][j]; 51 | if(x >= i) { 52 | ok = 1; 53 | ans += ('a' + j); 54 | last_i = pos[last_i][j] + 1; 55 | break; 56 | } 57 | } 58 | if(!ok) return "-1"; 59 | } 60 | return ans; 61 | } 62 | 63 | void solve() 64 | { 65 | ll x; 66 | string s; 67 | cin >> s >> x; 68 | cout << f(s, x) << endl; 69 | } 70 | ``` 71 |
72 | 73 |
74 | code (2) c++ 75 | 76 | ```cpp 77 | // Time: O(len * 26) 78 | // Space: O(n) 79 | 80 | string f(string s, ll len) 81 | { 82 | ll n = s.size(); 83 | vector> pos(26); 84 | for(ll i = 0; i < n; i++) { 85 | pos[s[i] - 'a'].push_back(i); 86 | } 87 | for(ll i = 0; i < 26; i++) { 88 | reverse(pos[i].begin(), pos[i].end()); 89 | } 90 | 91 | ll last_i = 0; 92 | string ans = ""; 93 | for(ll i = len; i >= 1; i--) 94 | { 95 | bool ok = 0; 96 | for(ll j = 0; j < 26; j++) 97 | { 98 | while(!pos[j].empty() and pos[j].back() < last_i) { 99 | pos[j].pop_back(); 100 | } 101 | if(!pos[j].empty()) { 102 | ll x = n - pos[j].back(); 103 | if(x >= i) { 104 | ok = 1; 105 | ans += ('a' + j); 106 | last_i = pos[j].back() + 1; 107 | break; 108 | } 109 | } 110 | } 111 | if(!ok) return "-1"; 112 | } 113 | return ans; 114 | } 115 | 116 | void solve() 117 | { 118 | ll x; 119 | string s; 120 | cin >> s >> x; 121 | cout << f(s, x) << endl; 122 | } 123 | ``` 124 |
-------------------------------------------------------------------------------- /Problems/greedy/2.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an array of size n. 4 | find size of max subset such that AND of all elements of that subset > 0 5 | AND = both bit set then 1 else 0 6 | 7 | Constraints: 8 | n <= 1e5 9 | a[i] <= 1e9 10 | 11 | Input: 12 | a = [5, 2, 1] 13 | 14 | Output: 15 | 2 16 | 17 | Explanation: 18 | [5, 1] is valid longest subset 19 | ``` 20 | **asked in microsoft** 21 | 22 | ### Solution 23 | ``` 24 | we just want AND > 0 25 | AND > 0 means their is atleast one bit set in all choosen elements of subset 26 | ``` 27 | 28 |
29 | code c++ 30 | 31 | ```cpp 32 | using ll = long long int; 33 | ll f(vector a) 34 | { 35 | ll ans = 0; 36 | ll n = a.size(); 37 | for(ll bit = 0; bit < 32; bit++) 38 | { 39 | ll cur = 0; 40 | for(ll j = 0; j < n; j++) 41 | { 42 | if(a[j] & (1ll << bit)) 43 | { 44 | cur++; 45 | } 46 | } 47 | ans = max(ans, cur); 48 | } 49 | return ans; 50 | } 51 | ``` 52 |
-------------------------------------------------------------------------------- /Problems/implementation/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Create a data structure that performs all the following operations in O(1) time: 4 | 5 | plus: Add a key with value 1. If the key already exists, increment its value by one. 6 | minus: Decrement the value of a key. If the key's value is currently 1, remove it. 7 | get_max: Return a key with the highest value. 8 | get_min: Return a key with the lowest value. 9 | ``` 10 | https://leetcode.com/problems/all-oone-data-structure/ 11 | 12 | ### Solution 13 | ``` 14 | check out leetcode discussion :) 15 | ``` 16 | -------------------------------------------------------------------------------- /Problems/linkedlist/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given linked list head pointer. Length of the LinkedList is unknown. 4 | Given two integers N and M. 5 | 6 | print ((N / M) * (length of LinkedList))th node data without calculating length of LinkedList. 7 | (only in single traversal of linked lists). 8 | 9 | NOTE: (N <= M) and (length of LinkedList is divisible by M) 10 | so, ((N / M) * (Length of LinkedList)) is always an integer number. 11 | 12 | Ex: 13 | N = 3, M = 4 14 | LinkedList is: 1-> 2-> 3-> 4-> 5-> 6-> 7-> 8-> 9-> 10-> 11-> 12 15 | (3 / 4) * 12 = 9th node data = 9 16 | 17 | N = 2, M = 3 18 | LinkedList: 6-> 8-> 9-> 14-> 67-> 5-> 7-> 56-> 56 19 | (2 / 3) * (9) = 6th node data = 5 20 | ``` 21 | 22 | ### Solution 23 | https://pastebin.com/nkx7k9GY 24 | 25 |
26 | code in c++ 27 | 28 | ```cpp 29 | /* 30 | Given linked list head pointer. Length of the LinkedList is unknown. 31 | Given two integers N and M. 32 | 33 | print ((N / M) * (length of LinkedList))th node data without calculating length of LinkedList. (only in single traversal of linked list). 34 | 35 | NOTE: (N <= M) and (length of LinkedList is divisible by M) 36 | so, ((N / M) * (Length of LinkedList)) is always an integer number. 37 | 38 | Ex: 39 | N = 3, M = 4 40 | LinkedList is: 1-> 2-> 3-> 4-> 5-> 6-> 7-> 8-> 9-> 10-> 11-> 12 41 | (3 / 4) * 12 = 9th node data = 9 42 | 43 | N = 2, M = 3 44 | LinkedList: 6-> 8-> 9-> 14-> 67-> 5-> 7-> 56-> 56 45 | (2 / 3) * (9) = 6th node data = 5 46 | */ 47 | 48 | #include 49 | using namespace std; 50 | 51 | #define ll long long int 52 | #define endl '\n' 53 | 54 | struct Linkedlist 55 | { 56 | ll val; 57 | Linkedlist *next; 58 | Linkedlist() { 59 | val = -1; 60 | next = NULL; 61 | } 62 | }; 63 | 64 | ll Solve(Linkedlist* head, ll n, ll m) 65 | { 66 | ll len = 0; 67 | Linkedlist* l = NULL; 68 | Linkedlist* node = head; 69 | 70 | while(node != NULL) 71 | { 72 | len++; 73 | if(len % m == 0) 74 | { 75 | ll cnt = n; 76 | while(cnt > 0) 77 | { 78 | if(l == NULL) { 79 | l = head; 80 | } 81 | else { 82 | l = l -> next; 83 | } 84 | cnt--; 85 | } 86 | } 87 | node = node -> next; 88 | } 89 | return l -> val; 90 | } 91 | void printLinkedList(Linkedlist *head) 92 | { 93 | while(head != NULL) { 94 | cout << (head -> val) << " "; 95 | head = head -> next; 96 | } 97 | cout << "\n"; 98 | } 99 | 100 | void solve(int &tc) 101 | { 102 | auto createList = [](vector &a) 103 | { 104 | Linkedlist *node = NULL, *head = NULL; 105 | bool first = true; 106 | for(ll x : a) 107 | { 108 | if(first) { 109 | node = new Linkedlist(); 110 | head = node; 111 | first = false; 112 | } 113 | else { 114 | node -> next = new Linkedlist(); 115 | node = node -> next; 116 | } 117 | node -> val = x; 118 | node -> next = NULL; 119 | } 120 | return head; 121 | }; 122 | 123 | vector a = {6, 8, 9, 14, 67, 5, 7, 56, 56}; 124 | Linkedlist *head = createList(a); 125 | cout << Solve(head, 2, 3) << endl; 126 | } 127 | 128 | int main() 129 | { 130 | ios::sync_with_stdio(false); 131 | cin.tie(0); 132 | 133 | int tc = 1; 134 | for(int i = 1; i <= tc; i++) { 135 | solve(i); 136 | } 137 | return 0; 138 | } 139 | ``` 140 |
-------------------------------------------------------------------------------- /Problems/lowest common ancestor/readme.md: -------------------------------------------------------------------------------- 1 | ### LCA 2 | 3 | **problems** 4 | - https://www.spoj.com/problems/LCASQ/ 5 | - https://www.spoj.com/problems/LCA/ 6 | - https://www.codechef.com/CCHI2021/problems/CCBTS05 7 | - https://www.codechef.com/problems/TALCA 8 | - https://www.codechef.com/problems/MOAT 9 | - https://www.codechef.com/problems/PSHTTR 10 | - https://codeforces.com/problemset/problem/609/E 11 | - https://www.spoj.com/problems/DISQUERY/ 12 | - https://www.spoj.com/problems/QTREE2/ 13 | - https://www.codechef.com/problems/TREDIFF 14 | - https://cses.fi/problemset/task/1136 15 | - https://codeforces.com/contest/191/problem/C 16 | - https://www.spoj.com/problems/ADAORANG/ 17 | - https://www.codechef.com/problems/TREEDGE -------------------------------------------------------------------------------- /Problems/maths/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given f6(), return random number between 1 to 6 with equal probability 4 | Design f18(). 5 | 6 | int f18() 7 | { 8 | // using f6() 9 | // which will return random number between 1 to 18 with equal probability 10 | } 11 | ``` 12 | 13 | ### Solution 14 | ``` 15 | 1 2 3 4 5 6 16 | 7 8 9 10 11 12 17 | 13 14 15 16 17 18 18 | 1 2 3 4 5 6 19 | 7 8 9 10 11 12 20 | 13 14 15 16 17 18 21 | 22 | 6 * 6 matrix 23 | return mat[f6()-1][f6()-1] 24 | ``` 25 | 26 | ### Bonus 27 | ``` 28 | do it without creating matrix (do some maths) 29 | ``` 30 | 31 | ### Similar problem 32 | ``` 33 | Given coin (equal probability of head, tail), implement unbiased coin with 34 | head probability (1/4) and tail (3/4) probability. 35 | 36 | Solution. 37 | [H, T] 38 | [T, T] 39 | Return mat[rand2()][rand2()] 40 | // rand2() return random value between [0, 1] (head or tail) 41 | ``` 42 | https://leetcode.com/problems/implement-rand10-using-rand7/ -------------------------------------------------------------------------------- /Problems/maths/2.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given array, count number of subsets whose product can be represented as product of unique prime numbers. 4 | 5 | 1 <= N <= 1e4 6 | 2 <= a[i] <= 30 7 | 8 | Arr = [4 2 3 15] 9 | Ans = 5 10 | 2 11 | 3 12 | 15 13 | 2, 15 14 | 2, 3 15 | 16 | Arr = [4 2 6 15] 17 | Ans = 4 18 | 2 19 | 6 20 | 15 21 | 2, 15 22 | ``` 23 | **Asked in lowe's india 2021** 24 | 25 | ### Solution 26 |
27 | code in c++ 28 | 29 | ```cpp 30 | #include 31 | using namespace std; 32 | 33 | using ll = long long int; 34 | #define endl '\n' 35 | 36 | const vector primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}; 37 | vector> pf(31); 38 | 39 | void prime_fact_init() { 40 | for(ll i = 2; i <= 30; i++) { 41 | pf[i].clear(); 42 | ll x = i; 43 | for(ll p : primes) { 44 | while(x > 0 and x % p == 0) { 45 | pf[i].push_back(p); 46 | x /= p; 47 | } 48 | } 49 | } 50 | } 51 | 52 | // return true if product of array = product of unique prime numbers 53 | bool product_ok(const vector& a) { 54 | map unique_primes; 55 | for(ll x : a) { 56 | for(ll p : pf[x]) { 57 | if(unique_primes[p]) { 58 | return 0; 59 | } 60 | unique_primes[p] = 1; 61 | } 62 | } 63 | return 1; 64 | } 65 | 66 | ll f(vector a) { 67 | // remove unnecessary number 68 | vector b; 69 | for(ll x : a) { 70 | if(product_ok({x})) { 71 | b.push_back(x); 72 | } 73 | } 74 | 75 | // find frequency 76 | map fre; 77 | for(ll x : b) { 78 | fre[x]++; 79 | } 80 | 81 | // remove duplicate 82 | sort(b.begin(), b.end()); 83 | b.erase(unique(b.begin(), b.end()), b.end()); 84 | 85 | // brute force calculate answer 86 | ll n = b.size(); 87 | ll ans = 0; 88 | for(ll i = 1; i < (1ll << n); i++) { 89 | ll cur = 1; 90 | vector cur_set; 91 | for(ll j = 0; j < n; j++) { 92 | if(i & (1ll << j)) { 93 | cur_set.push_back(b[j]); 94 | cur *= fre[b[j]]; 95 | } 96 | } 97 | if(product_ok(cur_set)) { 98 | ans += cur; 99 | } 100 | } 101 | return ans; 102 | } 103 | 104 | ll bf(vector a) { 105 | // remove unnecessary number 106 | vector b; 107 | for(ll x : a) { 108 | if(product_ok({x})) { 109 | b.push_back(x); 110 | } 111 | } 112 | 113 | ll n = b.size(); 114 | ll ans = 0; 115 | for(ll i = 1; i < (1ll << n); i++) { 116 | ll cur = 1; 117 | vector cur_set; 118 | for(ll j = 0; j < n; j++) { 119 | if(i & (1ll << j)) { 120 | cur_set.push_back(b[j]); 121 | cur *= 1; 122 | } 123 | } 124 | if(product_ok(cur_set)) { 125 | ans += cur; 126 | } 127 | } 128 | return ans; 129 | } 130 | 131 | void solve(ll &tc) 132 | { 133 | ll n; 134 | cin >> n; 135 | 136 | vector a(n); 137 | for(ll i = 0; i < n; i++) { 138 | cin >> a[i]; 139 | } 140 | 141 | assert(bf(a) == f(a)); 142 | cout << f(a) << endl; 143 | } 144 | 145 | int main() 146 | { 147 | ios::sync_with_stdio(0); 148 | cin.tie(0); 149 | 150 | prime_fact_init(); 151 | ll tc = 1; 152 | for(ll i = 1; i <= tc; i++) { 153 | solve(i); 154 | } 155 | return 0; 156 | } 157 | ``` 158 |
159 | 160 | Above code gives TLE here (`18 * 2 ^ 18 * some constant time`), there is another `28 * 2 ^ 10` solution 161 | https://leetcode.com/problems/the-number-of-good-subsets/ 162 | -------------------------------------------------------------------------------- /Problems/maths/3_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an array of size n and an integer m. Our task is to find the prefix sum of the array m number of times repeatedly. And print the array after it. 4 | 5 | Sample Input 6 | 2 7 | 3 2 8 | 1 2 3 9 | 5 3 10 | 1 2 3 4 5 11 | 12 | Sample Output 13 | 1 4 10 14 | 1 5 15 35 70 15 | 16 | Explanation:- 17 | In the second sample, the array goes like 18 | 1st time - 1 3 6 10 15 19 | 2nd time - 1 4 10 20 35 20 | 3rd time - 1 5 15 35 70 which is the output. 21 | 22 | constraints 23 | 1 <= T <= 20 24 | 2 <= N <= 1000 25 | 1 <= M <= 1000000000 26 | ``` 27 | **HP coding Round question** 28 | 29 | ### Solution 30 | ``` 31 | :( 32 | ``` 33 | 34 | - https://www.hackerearth.com/problem/algorithm/chocolates-9-fa64ad7d/ 35 | - https://codeforces.com/blog/entry/92223 -------------------------------------------------------------------------------- /Problems/maths/4_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | 1) Given array, count total pairs (i, j) such that i < j and gcd(a[i], a[j]) == 1 4 | 2) Given array, count total pairs (i, j) such that i < j and gcd(a[i], a[j]) > 1 5 | 3) Given 2 arrays(a1, a2), count total pairs (i, j) such that gcd(a1[i], a2[j]) > 1 6 | ``` 7 | 8 | ### Solution 9 | ``` 10 | idk :( 11 | Hint: Mobius function, Inclusion Exclusion principle 12 | ``` -------------------------------------------------------------------------------- /Problems/maths/5_unsolved.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Write a program that accepts a number n and outputs the sum of all the digits in the decimal representation of n!. 4 | For example: if n is 10, n! = 10! = 3628800. The output should be 27 in this case. 5 | ``` 6 | 7 | **Constraints** 8 | ``` 9 | 1 <= n <= 2000 10 | ``` 11 | 12 | **Input** 13 | ``` 14 | 9 15 | ``` 16 | 17 | **Output** 18 | ``` 19 | 27 20 | ``` 21 | 22 | **Explanation** 23 | ``` 24 | 9! = 362880 25 | sum of digit = 3 + 6 + 2 + 8 + 8 = 27 26 | ``` 27 | 28 | ### Solution 29 | ``` 30 | :( 31 | ``` 32 | 33 |
34 | code 35 | 36 | ``` 37 | your code here 38 | ``` 39 |
-------------------------------------------------------------------------------- /Problems/maths/6.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | There is an array of length N. Some places are empty (E), some are dirty (D), and some have food on it (F). 4 | You have to group all the food into consecutive cells with the following rules: 5 | — Moving food to adjacent cell costs 'x' amount of coins 6 | — Cleaning a dirty cell costs 'y' coins 7 | — After cleaning, the dirty cell becomes empty 8 | — You can only move food to an adjacent empty cell 9 | 10 | Now, you have to process Q queries of the type "x y". In each query, print the minimum cost required to group all foods together. 11 | ``` 12 | 13 | **Constraints** 14 | ``` 15 | 1 <= N, Q <= 1e5 16 | 0 <= x, y <= 1e5 17 | ``` 18 | 19 | **Input** 20 | ``` 21 | 5 22 | FEDEF 23 | 1 24 | 1 2 25 | ``` 26 | 27 | **Output** 28 | ``` 29 | 5 30 | ``` 31 | 32 | **Explanation** 33 | ``` 34 | N = 5, Array = "FEDEF" 35 | Q = 1, first query x = 1, y = 2 36 | It is optimal to first clean the dirty cell at position 3, this will cost 2 amount of coins. 37 | After cleaning the array becomes — "FEEEF". Now we can move foods in the following manner — "EFFEE". This will cost 3 * 1 = 3 coins. 38 | So final cost will be 3 + 2 = 5 39 | ``` 40 | 41 | ### Tag 42 | `Medium`, `Intuit` 43 | 44 | ### Solution 45 | try this https://codeforces.com/contest/1520/problem/E 46 | ``` 47 | Find the first and last occurrence of 'F', let them be l and r. 48 | We don't care about anything before l and after r. 49 | Let cnt = total number of F. 50 | Make all the D's in between l and r to E using y coins for each. 51 | Now the problem is same as https://codeforces.com/contest/1520/problem/E 52 | ``` 53 | 54 |
55 | code (c++) 56 | 57 | ```cpp 58 | #include 59 | using namespace std; 60 | 61 | int main() { 62 | int n, q; cin >> n >> q; 63 | string s; cin >> s; 64 | int l, r; 65 | for(int i = 0; i < n; i++) { // first food position 66 | if(s[i] == 'F') { 67 | l = i; 68 | break; 69 | } 70 | } 71 | for(int i = n - 1; i >= 0; i--) { // last food position 72 | if(s[i] == 'F') { 73 | r = i; 74 | break; 75 | } 76 | } 77 | int dirt = 0, food = 0; 78 | for(int i = l; i <= r; i++) { 79 | if(s[i] == 'D') dirt++; 80 | if(s[i] == 'F') food++; 81 | } 82 | int mid = food / 2 + food % 2; // it is always optimal to gather the food around the median 83 | int cnt = 0, idx; 84 | for(int i = l; i <= r; i++) { 85 | if(s[i] == 'F') cnt++; 86 | if(cnt == mid) { 87 | idx = i; 88 | break; 89 | } 90 | } 91 | //before mid 92 | int prev = 0, shift = 0; 93 | cnt = 0; 94 | for(int i = l; i < idx; i++) { 95 | if(s[i] == 'F') cnt++; 96 | else prev += cnt; 97 | } 98 | // after mid 99 | cnt = 0, shift = prev, prev = 0; 100 | for(int i = r; i >= idx; i--) { 101 | if(s[i] == 'F') cnt++; 102 | else prev += cnt; 103 | } 104 | shift += prev; 105 | while(q--) { 106 | int x, y; cin >> x >> y; // x -> shifting food to adjacent , y -> cleaning the dirt 107 | cout << (dirt * y) + (shift * x) << "\n"; 108 | } 109 | } 110 | ``` 111 |
-------------------------------------------------------------------------------- /Problems/prefix sum/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an array of integers, print out an array where at each index i, the total 4 | distance from i to every other duplicate element of i in the array is shown. 5 | 6 | Constraints: 7 | n <= 1e5 8 | a[i] <= 1e9 9 | 10 | Input: 11 | n = 5 12 | a = {1, 3, 1, 1, 2} 13 | 14 | Output: 15 | ans = {5, 0, 3, 4, 0} 16 | 17 | Explanation: 18 | index 19 | 0 => (2 - 0) + (3 - 0) = 5 20 | 1 => 0 21 | 2 => (2 - 0) + (3 - 2) = 3 22 | 3 => (3 - 0) + (3 - 2) = 4 23 | 4 => 0 24 | ``` 25 | 26 | ### Solution 27 | ``` 28 | Hint: prefix, suffix ? 29 | ``` 30 | 31 |
32 | code c++ 33 | 34 | ```cpp 35 | vector f(vector a) 36 | { 37 | ll n = a.size(); 38 | map> pos; 39 | for(ll i = 0; i < n; i++) { 40 | pos[a[i]].push_back(i); 41 | } 42 | 43 | vector ans(n, -1); 44 | for(auto &[k, v] : pos) { 45 | ll sz = v.size(); 46 | vector pre(sz, 0), suf(sz, 0); 47 | for(ll i = 1; i < sz; i++) { 48 | pre[i] = pre[i - 1] + i * (v[i] - v[i - 1]); 49 | } 50 | for(ll i = sz - 2; i >= 0; i--) { 51 | suf[i] = suf[i + 1] + (sz - 1 - i) * (v[i + 1] - v[i]); 52 | } 53 | for(ll i = 0; i < sz; i++) { 54 | ans[v[i]] = pre[i] + suf[i]; 55 | } 56 | } 57 | return ans; 58 | } 59 | 60 | void solve(ll &tc) 61 | { 62 | vector a = {1, 3, 1, 1, 2}; 63 | vector ans = f(a); 64 | for(ll& x : ans) { 65 | cout << x << " "; 66 | } 67 | cout << endl; 68 | } 69 | ``` 70 |
-------------------------------------------------------------------------------- /Problems/prefix sum/2.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Given an array of n elements. you can perform the below operation at most once. 4 | operation: pick any element in an array and change it to 0. 5 | you are required to maximize the number of partitions of the given array such that 6 | after the partition, sum of left subarray = sum of right subarray. 7 | (partition can't be empty) 8 | 9 | ex. a = {0, 5, 5} then {0, 5}, {5} is valid partition but {0}, {5, 5} is not. 10 | ``` 11 | 12 | **Constraints** 13 | ``` 14 | 1 <= test cases <= 10 15 | 1 <= n <= 10^5 16 | -10^9 <= a[i] <= 10^9 17 | ``` 18 | 19 | **Input** 20 | ``` 21 | 1 22 | 6 23 | -1 5 0 0 5 0 24 | ``` 25 | 26 | **Output** 27 | ``` 28 | 3 29 | ``` 30 | 31 | **Explanation** 32 | ``` 33 | 1st test case 34 | a = {-1, 5, 0, 0, 5, 0} 35 | change -1 to 0 36 | a = {0, 5, 0, 0, 5, 0} 37 | 38 | all possible partition 39 | {0, 5}, {0, 0, 5, 0} 40 | {0, 5, 0}, {0, 5, 0} 41 | {0, 5, 0, 0}, {5, 0} 42 | answer = 3 43 | ``` 44 | 45 | ### Tag 46 | `google`, `prefix-suffix`, `easy` 47 | 48 | ### Solution 49 | ``` 50 | for each index i, make a[i] = 0 51 | count answer using prefix and suffix sum count and maximize answer. 52 | ``` 53 | 54 |
55 | code c++ 56 | 57 | ```cpp 58 | ll f(vector a) 59 | { 60 | ll n = a.size(); 61 | if(n <= 1) { 62 | return 0; 63 | } 64 | ll sum = 0; 65 | map pre, suf; 66 | for(ll i = n - 1; i >= 0; i--) { 67 | sum += a[i]; 68 | suf[sum]++; 69 | } 70 | ll ans = 0; 71 | if(sum % 2 == 0) { 72 | ans = max(0ll, suf[sum / 2] - (sum == 0)); 73 | } 74 | for(ll i = 0, pr = 0, sf = sum; i < n; i++) { 75 | suf[sf]--; 76 | sf -= a[i]; 77 | // change a[i] to 0 78 | pre[pr + 0]++; 79 | ll n_sum = sum - a[i]; 80 | if(n_sum % 2 == 0) { 81 | // pre => (0, i) 82 | // suf => (i + 1, n - 1) 83 | ll cur = pre[n_sum / 2] + suf[n_sum / 2]; 84 | if(pr * 2 == n_sum) { 85 | cur--; 86 | } 87 | ans = max(ans, cur); 88 | } 89 | pre[pr + 0]--; 90 | pr += a[i]; 91 | pre[pr]++; 92 | } 93 | return ans; 94 | } 95 | 96 | void solve() 97 | { 98 | ll n; 99 | cin >> n; 100 | 101 | vector a(n); 102 | for(ll i = 0; i < n; i++) { 103 | cin >> a[i]; 104 | } 105 | 106 | cout << f(a) << endl; 107 | } 108 | ``` 109 |
-------------------------------------------------------------------------------- /Problems/prefix sum/3.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Find index in array such that all elements left to it are smaller and all elements right to it are bigger. 4 | ``` 5 | 6 | **Constraints** 7 | ``` 8 | 1 <= n <= 1e5 9 | -1e9 <= a[i] <= 1e9 10 | ``` 11 | 12 | **Input** 13 | ``` 14 | 10 15 | 5 4 3 2 1 6 7 8 9 10 16 | ``` 17 | 18 | **Output** 19 | ``` 20 | 5 21 | Note: 0 based index 22 | ``` 23 | 24 | **Explanation** 25 | ``` 26 | not needed right? 27 | ``` 28 | 29 | ### Tag 30 | `Easy` 31 | 32 | ### Solution 33 | ``` 34 | think of some prefix, suffix property :) 35 | ``` 36 | 37 |
38 | code (c++) 39 | 40 | ```cpp 41 | ll solve() 42 | { 43 | ll n; 44 | cin >> n; 45 | 46 | ll a[n]; 47 | for(ll i = 0; i < n; i++) { 48 | cin >> a[i]; 49 | } 50 | 51 | ll pre_max[n], suf_min[n]; 52 | 53 | pre_max[0] = a[0]; 54 | for(ll i = 1; i < n; i++) { 55 | pre_max[i] = max(pre_max[i - 1], a[i]); 56 | } 57 | 58 | suf_min[n - 1] = a[n - 1]; 59 | for(ll i = n - 2; i >= 0; i--) { 60 | suf_min[i] = min(suf_min[i + 1], a[i]); 61 | } 62 | 63 | for(ll i = 0; i < n; i++) { 64 | if(i == 0) { 65 | if(i + 1 < n and a[i] < suf_min[1]) { 66 | return i; 67 | } 68 | } 69 | else if(i == n - 1) { 70 | if(i - 1 >= 0 and a[i] > pre_max[i - 1]) { 71 | return i; 72 | } 73 | } 74 | else if(pre_max[i - 1] < a[i] and a[i] < suf_min[i + 1]) { 75 | return i; 76 | } 77 | } 78 | return -1; 79 | } 80 | ``` 81 |
-------------------------------------------------------------------------------- /Problems/prefix sum/4.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | You are given an array of strings (size N). 4 | You have to process Q queries of the type — "L R k". For each query, 5 | print the kth character of the string formed by merging all the strings from L to R and sorting them. 6 | ``` 7 | 8 | **Constraints** 9 | ``` 10 | 1 <= N <= 1e5 11 | 1 <= Q <= 1e5 12 | 1 <= L <= R <= N 13 | There is always a valid kth position 14 | ``` 15 | 16 | **Input** 17 | ``` 18 | 3 19 | abcc trea zape 20 | 2 21 | 2 3 5 22 | 2 2 1 23 | ``` 24 | 25 | **Output** 26 | ``` 27 | p 28 | a 29 | ``` 30 | 31 | **Explanation** 32 | ``` 33 | N = 3, strings = ["abcc", "trea", "zape"] 34 | Q = ["2 3 5", "2 2 1"] 35 | 36 | For first query, concatenated string is "treazape". 37 | After sorting, it becomes "aaeeprtz". 5th character is 'p'. 38 | 39 | For second query, concatenated string is "trea". 40 | After sorting becomes "aert". 1st character is 'a'. 41 | ``` 42 | 43 | ### Tag 44 | `Easy`, `Prefix-sum`, `Intuit` 45 | 46 | ### Solution 47 | ``` 48 | we can maintain just prefix sums for each character. ex. pre[index][char]. 49 | Then for each query, we can just iterate over all characters from 'a' to 'z' 50 | and have total characters till now and when it becomes >= K, we got our answer 51 | for the query as the current character. 52 | 53 | Time complexity: O(Q * 26) 54 | Space complexity: O(N * 26) 55 | ``` 56 | 57 |
58 | code (c++) 59 | 60 | ``` 61 | can't you try by yourself ? 62 | it's easy. 63 | ``` 64 |
-------------------------------------------------------------------------------- /Problems/prefix sum/readme.md: -------------------------------------------------------------------------------- 1 | ### Prefix Sum and Diffrence array 2 | 3 | **learn** 4 | - https://www.geeksforgeeks.org/prefix-sum-array-implementation-applications-competitive-programming/ 5 | - https://codeforces.com/blog/entry/78762 6 | - https://codeforces.com/blog/entry/86420 7 | 8 | **problems** 9 | - https://www.codechef.com/problems/RSP 10 | - https://www.hackerrank.com/challenges/crush/problem 11 | - https://leetcode.com/problems/find-pivot-index/ 12 | - https://www.codechef.com/problems/NUM239 13 | - https://www.hackerearth.com/problem/algorithm/prefix-array-strength/ 14 | - https://www.codechef.com/problems/AGCY 15 | - https://www.codechef.com/CENS2020/problems/CENS20A 16 | - https://codeforces.com/contest/1539/problem/B 17 | - https://leetcode.com/problems/minimum-absolute-difference-queries/ 18 | - https://codeforces.com/problemset/problem/276/C 19 | - https://codeforces.com/contest/276/problem/C 20 | - https://codeforces.com/contest/816/problem/B 21 | - https://codeforces.com/contest/177/problem/D2 22 | - https://codeforces.com/contest/295/problem/A 23 | - https://codeforces.com/problemset/problem/1343/D -------------------------------------------------------------------------------- /Problems/problem_structure.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | problem statement goes here ... 4 | ``` 5 | 6 | **Constraints** 7 | ``` 8 | ``` 9 | 10 | **Input** 11 | ``` 12 | ``` 13 | 14 | **Output** 15 | ``` 16 | ``` 17 | 18 | **Explanation** 19 | ``` 20 | ``` 21 | 22 | ### Tag 23 | `easy`, `google` 24 | 25 | ### Solution 26 | ``` 27 | :) 28 | ``` 29 | 30 |
31 | code (c++) 32 | 33 | ``` 34 | your code here 35 | ``` 36 |
-------------------------------------------------------------------------------- /Problems/random practice problems/atcoder.md: -------------------------------------------------------------------------------- 1 | ### Atcoder 2 | 3 | - https://atcoder.jp/contests/abc172/tasks/abc172_c -------------------------------------------------------------------------------- /Problems/random practice problems/codeforces.md: -------------------------------------------------------------------------------- 1 | ### Codeforces 2 | 3 | - https://codeforces.com/contest/982/problem/B 4 | - https://codeforces.com/problemset/problem/525/B 5 | - https://codeforces.com/contest/1536/problem/C -------------------------------------------------------------------------------- /Problems/random practice problems/hackerrank.md: -------------------------------------------------------------------------------- 1 | ### Hackerrank 2 | 3 | - https://www.hackerrank.com/challenges/counting-valleys/problem 4 | - https://www.hackerrank.com/challenges/sherlock-and-cost/problem 5 |
6 | Code in c++ 7 | 8 | ```cpp 9 | void solve() 10 | { 11 | int n; 12 | cin >> n; 13 | 14 | int b[n]; 15 | for(int i = 0; i < n; i++) { 16 | cin >> b[i]; 17 | } 18 | 19 | int dp[n][2]; 20 | // dp[i][0] => max answer if a[i] = 1 21 | // dp[i][1] => max answer if a[i] = b[i] 22 | dp[0][0] = dp[0][1] = 0; 23 | 24 | for(int i = 1; i < n; i++) { 25 | dp[i][0] = max( 26 | dp[i - 1][0] + abs(1 - 1), 27 | dp[i - 1][1] + abs(1 - b[i - 1]) 28 | ); 29 | dp[i][1] = max( 30 | dp[i - 1][0] + abs(b[i] - 1), 31 | dp[i - 1][1] + abs(b[i] - b[i - 1]) 32 | ); 33 | } 34 | cout << max(dp[n - 1][0], dp[n - 1][1]) << endl; 35 | } 36 | ``` 37 |
38 | 39 | - https://www.hackerrank.com/challenges/construct-the-array/problem -------------------------------------------------------------------------------- /Problems/random practice problems/leetcode.md: -------------------------------------------------------------------------------- 1 | ### Leetcode 2 | 3 | - https://leetcode.com/problems/trapping-rain-water/ -------------------------------------------------------------------------------- /Problems/segment tree/1.cpp: -------------------------------------------------------------------------------- 1 | // https://www.hackerearth.com/challenges/competitive/pycon-2020/algorithm/jumbo-and-array-1-69b5c314-2b740e3b/ 2 | 3 | #include 4 | using namespace std; 5 | 6 | #ifdef DHIRAJ 7 | #include "D:/dhiraj/Programming/debug.h" 8 | #else 9 | #define dbg(...) 1 10 | #define cerr if(0) cerr 11 | #endif 12 | 13 | using ll = long long int; 14 | #define endl '\n' 15 | 16 | template 17 | inline istream & operator >> (istream &in, pair &p) { 18 | in >> p.first >> p.second; 19 | return in; 20 | } 21 | template 22 | inline istream & operator >> (istream &in, vector &v) { 23 | for(T &x : v) in >> x; 24 | return in; 25 | } 26 | 27 | struct node { 28 | ll sm, smsq, ans; 29 | node(ll x = 0) { 30 | sm = x; 31 | smsq = x * x; 32 | ans = (sm * sm - smsq) / 2; 33 | }; 34 | friend string to_string(node n) 35 | { 36 | string res = ""; 37 | #ifdef DHIRAJ 38 | { 39 | res += "["; 40 | res += to_string(n.sm) + ","; 41 | res += to_string(n.smsq) + ","; 42 | res += to_string(n.ans); 43 | res += "]"; 44 | } 45 | #endif 46 | return res; 47 | } 48 | }; 49 | 50 | #define left(n) (n << 1LL) 51 | #define right(n) ((n << 1LL) | 1LL) 52 | 53 | template 54 | struct SegmentTree 55 | { 56 | ll n; T e; 57 | vector tree; 58 | function combine; 59 | 60 | SegmentTree(const vector &arr, ll _n, T _e, function _combine) { 61 | n = _n, e = _e; 62 | combine = _combine; 63 | tree.resize(n << 2ll); 64 | build(1, 1, n, arr); 65 | } 66 | 67 | void build(ll i, ll l, ll r, const vector &arr) { 68 | if(l == r) { // leaf node of segment tree 69 | tree[i] = arr[l]; 70 | return; 71 | } 72 | ll m = (l + r) >> 1ll; 73 | build(left(i), l, m, arr); 74 | build(right(i), m + 1, r, arr); 75 | tree[i] = combine(tree[left(i)], tree[right(i)]); 76 | } 77 | 78 | void update(ll i, ll l, ll r, ll ind, T val) { 79 | if(l == r) { 80 | tree[i] = val; 81 | return; 82 | } 83 | ll m = (l + r) >> 1ll; 84 | if(ind <= m) { 85 | update(left(i), l, m, ind, val); 86 | } 87 | else { 88 | update(right(i), m + 1, r, ind, val); 89 | } 90 | tree[i] = combine(tree[left(i)], tree[right(i)]); 91 | } 92 | 93 | T query(ll i, ll l, ll r, ll ql, ll qr) { 94 | if(l > r or r < ql or l > qr) { // (l, r) outside (ql, qr) 95 | return e; 96 | } 97 | if(ql <= l and r <= qr) { // (l, r) inside (ql, qr) 98 | return tree[i]; 99 | } 100 | ll m = (l + r) >> 1ll; 101 | T L = query(left(i), l, m, ql, qr); 102 | T R = query(right(i), m + 1, r, ql, qr); 103 | return combine(L, R); 104 | } 105 | 106 | void update(ll ind, T val) { 107 | update(1, 1, n, ind, val); 108 | } 109 | T query(ll l, ll r) { 110 | return query(1, 1, n, l, r); 111 | } 112 | }; 113 | 114 | template 115 | string to_string(SegmentTree s) { 116 | string res = "Segment Tree\n"; 117 | #ifdef DHIRAJ 118 | { 119 | res += "arr = {"; 120 | for(ll i = 1; i <= s.n; i++) { 121 | if(i > 1) res += ", "; 122 | res += to_string(s.query(i, i)); 123 | } 124 | res += "}\n"; 125 | res += "{\n"; 126 | res += "\tid\trange\tdata\n"; 127 | 128 | auto add_info = [&res](ll id, ll l, ll r, T info) { 129 | pair range = {l, r}; 130 | res += "\t" + to_string(id); 131 | res += "\t" + to_string(range); 132 | res += "\t" + to_string(info); 133 | res += "\n"; 134 | }; 135 | function F = [&](ll id, ll l, ll r) { 136 | if(l == r) { 137 | add_info(id, l, r, s.tree[id]); 138 | return; 139 | } 140 | ll m = (l + r) / 2; 141 | F(left(id), l, m); 142 | F(right(id), m + 1, r); 143 | add_info(id, l, r, s.tree[id]); 144 | }; 145 | F(1, 1, s.n); 146 | res += "}\n"; 147 | } 148 | #endif 149 | return res; 150 | } 151 | 152 | void solve(ll &tc) 153 | { 154 | ll n, q; 155 | cin >> n >> q; 156 | 157 | vector a(n + 1); 158 | for(ll i = 1; i <= n; i++) { 159 | ll x; 160 | cin >> x; 161 | a[i] = node(x); 162 | } 163 | 164 | // node { 165 | // sm => sum of element [l, r] 166 | // smsq => sum of square of element [l, r] 167 | // ans = (sm * sm - smsq) / 2 => ans of [l, r] 168 | // } 169 | 170 | 171 | SegmentTree s(a, n, node(), 172 | [](const node& l, const node& r) { 173 | node res; 174 | res.sm = l.sm + r.sm; 175 | res.smsq = l.smsq + r.smsq; 176 | res.ans = (res.sm * res.sm - res.smsq) / 2; 177 | return res; 178 | }); 179 | 180 | while(q--) 181 | { 182 | ll t, x, y; 183 | cin >> t >> x >> y; 184 | if(t == 0) { 185 | cout << s.query(x, y).ans << endl; 186 | } 187 | else { 188 | s.update(x, y); 189 | } 190 | } 191 | } 192 | 193 | int main() 194 | { 195 | #ifdef DHIRAJ 196 | freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); 197 | freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); 198 | freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); 199 | #endif 200 | 201 | ios::sync_with_stdio(0); 202 | cin.tie(0); 203 | 204 | ll rep = 1; 205 | while(rep--) { 206 | ll tc = 1; 207 | for(ll i = 1; i <= tc; i++) { 208 | // cout << "Case #" << i << ": "; 209 | cerr << "Case #" << i << "\n"; 210 | solve(i); 211 | } 212 | if(dbg()) break; 213 | } 214 | return 0; 215 | } -------------------------------------------------------------------------------- /Problems/segment tree/readme.md: -------------------------------------------------------------------------------- 1 | ### Segment tree 2 | 3 | **problems** 4 | - https://www.spoj.com/problems/RMQSQ/ 5 | - https://www.codechef.com/problems/FLIPCOIN 6 | - https://www.codechef.com/problems/GGSPAG 7 | - https://codeforces.com/contest/339/problem/D 8 | - https://www.spoj.com/problems/GSS1/ 9 | - https://www.spoj.com/problems/GSS3/ 10 | - https://www.spoj.com/problems/KGSS/ 11 | - https://www.spoj.com/problems/MULTQ3/ 12 | - https://codeforces.com/contest/52/problem/C 13 | - https://codeforces.com/contest/242/problem/E 14 | - https://www.spoj.com/problems/MKTHNUM/ 15 | - https://cses.fi/problemset/task/1137 16 | - https://cses.fi/problemset/task/1138/ 17 | - https://codeforces.com/contest/1450/problem/D 18 | - https://codeforces.com/contest/1440/problem/E 19 | - https://www.hackerrank.com/contests/daiict-winter-long-2021/challenges/standard-problem 20 | - https://www.spoj.com/problems/POSTERS/ 21 | - https://www.codechef.com/problems/OROFAND -------------------------------------------------------------------------------- /Problems/sqrt decomposition + mo's algo/readme.md: -------------------------------------------------------------------------------- 1 | ### Mo's algorithm | Sqrt decomposition 2 | 3 | **problems** 4 | - https://codeforces.com/contest/220/problem/B 5 | - https://codeforces.com/problemset/problem/86/D 6 | - https://www.codechef.com/problems/GGSPAG 7 | - https://www.spoj.com/problems/DQUERY/ 8 | - https://www.codechef.com/problems/GGSPAG/ 9 | - https://codeforces.com/contest/13/problem/E 10 | - https://www.spoj.com/problems/GIVEAWAY/ 11 | - https://codeforces.com/contest/1000/problem/F 12 | - https://codeforces.com/contest/375/problem/D 13 | - https://www.spoj.com/problems/GOT/ 14 | - https://cses.fi/problemset/task/1139 15 | - https://www.spoj.com/problems/FREQ2/ 16 | - https://codeforces.com/contest/1514/problem/D -------------------------------------------------------------------------------- /Problems/stack queue heap/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | Implement a stack API using only a heap. A stack implements the following methods: 4 | 5 | push(item), which adds an element to the stack 6 | pop(), which removes and returns the most recently added element (or throws an error if there is nothing on the stack) 7 | Recall that a heap has the following operations: 8 | 9 | push(item), which adds a new key to the heap 10 | pop(), which removes and returns the max value of the heap 11 | ``` 12 | 13 | ### Solution 14 | (1) 15 | ``` 16 | if item is positive intergers then 17 | in push we will push current max + item to heap and 18 | in pop we find 2nd max and return 1st max - 2nd max 19 | and remove 1st max 20 | 21 | ex. 22 | push 23 | 5 3 6 2 4 1 24 | 25 | heap at every step 26 | 5 27 | 5 8 28 | 5 8 14 29 | 5 8 14 16 30 | 5 8 14 16 20 31 | 5 8 14 16 20 21 32 | 33 | It can work for negative as well if you shift all the elements. 34 | I mean if range is -x to y then add x as well as top element of heap to the element being pushed. 35 | 36 | Basically shifting the numbers on the number line 37 | 38 | What I mean is if range is -x to y then add x to each element before pushing (before adding max) 39 | and remove x after pop (after removing second highest val) 40 | ``` 41 | 42 | (2) 43 | 44 | ``` 45 | we maintain two heap for positive and negative numbers 46 | 47 | in push(x) 48 | if x positive 49 | pos heap push(abs(x) + maximum in both heap till now) 50 | else 51 | neg heap push(abs(x) + maximum in both heap till now) 52 | 53 | in pop() 54 | if pos heap.top() >= neg heap top() 55 | then top of stack is postive number and 56 | top = pos.top() - second max in both heap 57 | 58 | else 59 | top of stack is negative number 60 | top = neg.top() - second max in both heap 61 | top = -top 62 | ``` 63 | https://pastebin.com/r867Pgki 64 | 65 |
66 | code in c++ 67 | 68 | ```cpp 69 | /* 70 | Implement a stack API using only a heap. A stack implements the following methods: 71 | 72 | push(item), which adds an element to the stack 73 | pop(), which removes and returns the most recently added element (or throws an error if there is nothing on the stack) 74 | Recall that a heap has the following operations: 75 | 76 | push(item), which adds a new key to the heap 77 | pop(), which removes and returns the max value of the heap 78 | */ 79 | 80 | #include 81 | using namespace std; 82 | 83 | struct Heap { 84 | multiset h; 85 | Heap() { 86 | h.clear(); 87 | } 88 | void push(int item) { 89 | h.insert(item); 90 | } 91 | int pop() { 92 | int top = *h.rbegin(); 93 | h.erase(--h.end()); 94 | return top; 95 | } 96 | int top() { 97 | return *h.rbegin(); 98 | } 99 | bool empty() { 100 | return h.empty(); 101 | } 102 | void print() { 103 | cout << "heap : "; 104 | for(int item : h) { 105 | cout << item << " "; 106 | } 107 | cout << endl; 108 | } 109 | void clear() { 110 | h.clear(); 111 | } 112 | }; 113 | 114 | struct Stack { 115 | Heap pos, neg; 116 | Stack() { 117 | pos.clear(); 118 | neg.clear(); 119 | pos.push(0); 120 | neg.push(0); 121 | } 122 | void push(int item) { 123 | int mx = max(pos.top(), neg.top()); 124 | if(item >= 0) { 125 | pos.push(mx + item); 126 | } 127 | else { 128 | neg.push(mx + (-item)); 129 | } 130 | } 131 | int pop() { 132 | int ans = 0; 133 | if(pos.top() >= neg.top()) { 134 | ans = pos.pop(); 135 | ans -= max(pos.top(), neg.top()); 136 | } 137 | else { 138 | ans = (-neg.pop()); 139 | ans += max(pos.top(), neg.top()); 140 | } 141 | return ans; 142 | } 143 | void print() { 144 | cout << "pos "; 145 | pos.print(); 146 | cout << "neg "; 147 | neg.print(); 148 | } 149 | }; 150 | 151 | int main() { 152 | Stack s; 153 | s.push(1); 154 | s.push(10); 155 | s.push(-1); 156 | s.print(); 157 | s.push(3); 158 | s.push(-5); 159 | cout << s.pop() << endl; 160 | s.print(); 161 | cout << s.pop() << endl; 162 | s.print(); 163 | cout << s.pop() << endl; 164 | cout << s.pop() << endl; 165 | cout << s.pop() << endl; 166 | s.push(-10); 167 | cout << s.pop() << endl; 168 | return 0; 169 | } 170 | ``` 171 |
-------------------------------------------------------------------------------- /Problems/stack queue heap/2.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | There are two people (p1 and p2). They are playing a game, where an array with 4 | some values is given. p1 makes the first turn, and p2 makes the 2nd turn, and 5 | this goes on. p1 play optimally, i.e. it always plays to select those value 6 | from the array which makes his sum maximum. p1 can pick any of the available 7 | value from the array. p2 does not play optimally. p2 simply picks the first 8 | available value (from the left). 9 | print "p1_score p2_score" both player score. 10 | ``` 11 | 12 | **Constraints** 13 | ``` 14 | 1 <= N <= 1e5 15 | 1 <= a[i] <= 1e9 16 | ``` 17 | 18 | **Input (1)** 19 | ``` 20 | 3 21 | 1 3 7 22 | ``` 23 | 24 | **Output (1)** 25 | ``` 26 | 10 1 27 | ``` 28 | 29 | **Explanation (1)** 30 | ``` 31 | a = {1, 3, 7} 32 | p1_score = p2_score = 0 33 | p1 take 3, p1_score = 3, p2_score = 0 34 | a = {1, 7} 35 | p2 take 1 (left most), p1_score = 3, p2_score = 1 36 | a = {7} 37 | p1 take 7, p1_score = 10, p2_score = 1 38 | a = {} 39 | 40 | answer: 10 1 41 | ``` 42 | 43 | **Input (2)** 44 | ``` 45 | 3 46 | 100 10 200 47 | ``` 48 | 49 | **Output (2)** 50 | ``` 51 | 300 10 52 | ``` 53 | 54 | **Explanation (2)** 55 | ``` 56 | a = {100, 10, 200} 57 | p1_score = p2_score = 0 58 | p1 take 100, p1_score = 100, p2_score = 0 59 | a = {10, 200} 60 | p2 take 10 (left most), p1_score = 100, p2_score = 10 61 | a = {200} 62 | p1 take 200, p1_score = 300, p2_score = 10 63 | a = {} 64 | 65 | answer: 300 10 66 | ``` 67 | 68 | ### Tag 69 | `Medium-Hard`, `Microsoft`, `Heap (priority queue)`, `Greedy` 70 | 71 | ### Solution 72 | ``` 73 | pure greedy doesn't work here (i mean, each time choose max element from here) .. 74 | ``` 75 | 76 |
77 | code (c++) 78 | 79 | ```cpp 80 | /* 81 | I am not sure this approch is correct or not :0 82 | Iterate array elements 83 | if(a[0] < a[1]) { 84 | p1 take max element from a[1] ... a[n] 85 | p2 take a[0] 86 | remove taken elements 87 | } 88 | else { 89 | p1 take a[0] 90 | p2 take a[1] 91 | remove taken elements 92 | } 93 | */ 94 | 95 | ll f(vector a) 96 | { 97 | ll n = a.size(); 98 | multiset> mx, id; 99 | for(ll i = 0; i < n; i++) { 100 | id.insert({i, a[i]}); 101 | mx.insert({a[i], i}); 102 | } 103 | 104 | ll p1 = 0, p2 = 0; 105 | auto add = [&](ll ind, ll val, bool player) { 106 | id.erase(id.find({ind, val})); 107 | mx.erase(mx.find({val, ind})); 108 | if(player) { 109 | p1 += val; 110 | } 111 | else { 112 | p2 += val; 113 | } 114 | }; 115 | 116 | for(ll i = 0; i < n; i += 2) { 117 | if(i + 1 < n) { 118 | auto [id1, val1] = *id.begin(); 119 | auto [id2, val2] = *(++id.begin()); 120 | if(val1 < val2) 121 | { 122 | auto [mx_ele, mx_id] = *(--mx.end()); 123 | add(mx_id, mx_ele, 1); 124 | add(id1, val1, 0); 125 | } 126 | else { 127 | add(id1, val1, 1); 128 | add(id2, val2, 0); 129 | } 130 | } 131 | else { 132 | auto [ind, val] = *id.begin(); 133 | add(ind, val, 1); 134 | } 135 | } 136 | cout << "p1 score: " << p1 << endl; 137 | cout << "p2 score: " << p2 << endl; 138 | return p1 - p2; 139 | } 140 | ``` 141 |
-------------------------------------------------------------------------------- /Problems/string/1.md: -------------------------------------------------------------------------------- 1 | ### Problem 2 | ``` 3 | given binary string and 2 integers X and Y. find how many substring have 4 | number of 0's in multiple of X and number of 1's in multiple of Y. 5 | ``` 6 | 7 | ### Solution 8 | ``` 9 | Hint: 10 | try count substring have number of 0's in multiple of X or 11 | try count substring have number of 1's in multiple of Y 12 | prefix sum ? 13 | ``` 14 | ```cpp 15 | void solve() 16 | { 17 | string s; 18 | cin >> s; 19 | 20 | int x, y; 21 | cin >> x >> y; 22 | 23 | map f; 24 | f[0] = 1; 25 | 26 | int ans = 0; 27 | int zr = 0; 28 | for(char c : s) 29 | { 30 | zr += (c == '0'); 31 | ans += f[zr % x]; 32 | f[zr % x]++; 33 | } 34 | cout << ans << endl; 35 | } 36 | ``` 37 | 38 |
39 | code in c++ 40 | 41 | ```cpp 42 | void solve() 43 | { 44 | string s; 45 | cin >> s; 46 | 47 | int x, y; 48 | cin >> x >> y; 49 | 50 | map, int> f; 51 | f[{0, 0}] = 1; 52 | 53 | int ans = 0; 54 | int ones = 0, zeros = 0; 55 | for(char c : s) 56 | { 57 | zeros += (c == '0'); 58 | ones += (c == '1'); 59 | ans += f[{zeros % x, ones % y}]; 60 | f[{zeros % x, ones % y}]++; 61 | } 62 | cout << ans << endl; 63 | } 64 | ``` 65 |
-------------------------------------------------------------------------------- /Problems/string/readme.md: -------------------------------------------------------------------------------- 1 | ### String algorithm 2 | 3 | **problems - string matching (kmp) | prefix suffix function (z algo) | manacher algo (palindrome)** 4 | - https://codeforces.com/contest/126/problem/B 5 | - https://codeforces.com/contest/1326/problem/D2 6 | - https://codeforces.com/contest/432/problem/D 7 | - https://codeforces.com/contest/1200/problem/E 8 | - https://www.spoj.com/problems/QUERYSTR/ 9 | - https://www.codechef.com/problems/INSQ15_A 10 | - https://codeforces.com/problemset/problem/471/D 11 | - https://codeforces.com/contest/1537/problem/E2 -------------------------------------------------------------------------------- /Problems/trie/readme.md: -------------------------------------------------------------------------------- 1 | ### Trie 2 | 3 | **problems** 4 | - http://codeforces.com/contest/706/problem/D 5 | - https://codeforces.com/contest/271/problem/D 6 | - http://codeforces.com/problemset/problem/948/D 7 | - http://codeforces.com/contest/665/problem/E 8 | - http://codeforces.com/contest/282/problem/E 9 | - https://codeforces.com/contest/842/problem/D 10 | - https://www.hackerrank.com/challenges/contacts/problem 11 | - https://www.hackerrank.com/challenges/no-prefix-set/problem -------------------------------------------------------------------------------- /Programming/1.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #ifdef DHIRAJ 5 | #include "D:/dhiraj/Programming/debug.h" 6 | #else 7 | #define dbg(...) 1 8 | #define cerr if(0) cerr 9 | #endif 10 | 11 | using ll = long long int; 12 | #define endl '\n' 13 | 14 | template 15 | inline istream& operator >> (istream& in, pair& p) { 16 | in >> p.first >> p.second; 17 | return in; 18 | } 19 | template 20 | inline istream& operator >> (istream& in, vector& v) { 21 | for(T& x : v) in >> x; 22 | return in; 23 | } 24 | 25 | void solve(ll &tc) { 26 | cout << "Hello World" << endl; 27 | } 28 | 29 | int main() 30 | { 31 | #ifdef DHIRAJ 32 | freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); 33 | freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); 34 | freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); 35 | #endif 36 | 37 | ios::sync_with_stdio(0); 38 | cin.tie(0); 39 | 40 | ll rep = 1; 41 | while(rep--) { 42 | ll tc = 1; 43 | cin >> tc; 44 | for(ll i = 1; i <= tc; i++) { 45 | // cout << "Case #" << i << ": "; 46 | cerr << "Case #" << i << "\n"; 47 | solve(i); 48 | } 49 | if(dbg()) break; 50 | } 51 | return 0; 52 | } -------------------------------------------------------------------------------- /Programming/README.md: -------------------------------------------------------------------------------- 1 | ### I Code [here](1.cpp) ❤ 2 | 3 | **Just for speed** 4 | 5 | (1) Parse complete testcase from any site 6 | https://github.com/jmerle/competitive-companion-example 7 | `C`, `C++` https://packagecontrol.io/packages/SublimeLinter-gcc 8 | 9 | (2) Sublime Linter (check erros while coding) 10 | http://www.sublimelinter.com/en/stable/index.html 11 | 12 | (3) Debug file `C++` 13 | https://github.com/dhiraj-01/CP/blob/main/Programming/debug.h 14 | 15 | (4) I use this to compare my output with actual output 16 | https://github.com/dhiraj-01/CP/blob/main/Sublime%20Text/Packages/Custom/compare.py 17 | -------------------------------------------------------------------------------- /Programming/debug.h: -------------------------------------------------------------------------------- 1 | #ifndef DEBUG_H 2 | #define DEBUG_H 3 | 4 | #include 5 | using namespace std; 6 | 7 | template 8 | string to_string(queue q); 9 | 10 | template 11 | string to_string(priority_queue pq); 12 | 13 | template 14 | string to_string(stack s); 15 | 16 | template 17 | string to_string(pair p); 18 | 19 | template 20 | string to_string(tuple t); 21 | 22 | template 23 | string to_string(tuple t); 24 | 25 | template 26 | string to_string(tuple t); 27 | 28 | template::value, typename T_container::value_type>::type> 29 | string to_string(T_container any); 30 | 31 | 32 | string to_string(char c) { 33 | string res(3, '\''); 34 | res[1] = c; 35 | return res; 36 | } 37 | string to_string(string a) { 38 | return "\"" + a + "\""; 39 | } 40 | string to_string(const char* s) { 41 | return to_string((string) s); 42 | } 43 | string to_string(bool a) { 44 | // return (a ? "true" : "false"); 45 | return (a ? "1" : "0"); 46 | } 47 | 48 | // bitset 49 | template 50 | string to_string(bitset v) { 51 | string res = ""; 52 | for(size_t i = 0; i < N; i++) { 53 | res += (v[i] ? '1' : '0'); 54 | } 55 | reverse(res.begin(), res.end()); 56 | return res; 57 | } 58 | 59 | // boolean can't access direclty in for each loop 60 | string to_string(vector v) { 61 | string res = ""; 62 | res += "{"; 63 | bool first = true; 64 | for (int i = 0; i < static_cast(v.size()); i++) { 65 | if (!first) res += ", "; 66 | first = false; 67 | res += to_string(v[i]); 68 | } 69 | res += "}"; 70 | return res; 71 | } 72 | 73 | // queue 74 | template 75 | string to_string(queue q) { 76 | string res = ""; 77 | res += "{"; 78 | bool first = true; 79 | while (!q.empty()) { 80 | if (!first) res += ", "; 81 | first = false; 82 | res += to_string(q.front()); 83 | q.pop(); 84 | } 85 | res += "}"; 86 | return res; 87 | } 88 | 89 | // priority_queue 90 | template 91 | string to_string(priority_queue pq) { 92 | string res = ""; 93 | res += "{"; 94 | bool first = true; 95 | while (!pq.empty()) { 96 | if (!first) res += ", "; 97 | first = false; 98 | res += to_string(pq.top()); 99 | pq.pop(); 100 | } 101 | res += "}"; 102 | return res; 103 | } 104 | 105 | // stack 106 | template 107 | string to_string(stack s) { 108 | stack rev; 109 | while (!s.empty()) { 110 | rev.emplace(s.top()); 111 | s.pop(); 112 | } 113 | 114 | string res = ""; 115 | res += "{"; 116 | bool first = true; 117 | while (!rev.empty()) { 118 | if (!first) res += ", "; 119 | first = false; 120 | res += to_string(rev.top()); 121 | rev.pop(); 122 | } 123 | res += "}"; 124 | return res; 125 | } 126 | 127 | // pair 128 | template 129 | string to_string(pair p) { 130 | string res = ""; 131 | res += "("; 132 | res += to_string(p.first) + ", "; 133 | res += to_string(p.second); 134 | res += ")"; 135 | return res; 136 | } 137 | 138 | // tuple 139 | template 140 | string to_string(tuple t) { 141 | string res = ""; 142 | res += "("; 143 | res += to_string(get<0>(t)) + ", "; 144 | res += to_string(get<1>(t)); 145 | res += ")"; 146 | return res; 147 | } 148 | 149 | template 150 | string to_string(tuple t) { 151 | string res = ""; 152 | res += "("; 153 | res += to_string(get<0>(t)) + ", "; 154 | res += to_string(get<1>(t)) + ", "; 155 | res += to_string(get<2>(t)); 156 | res += ")"; 157 | return res; 158 | } 159 | 160 | template 161 | string to_string(tuple t) { 162 | string res = ""; 163 | res += "("; 164 | res += to_string(get<0>(t)) + ", "; 165 | res += to_string(get<1>(t)) + ", "; 166 | res += to_string(get<2>(t)) + ", "; 167 | res += to_string(get<3>(t)); 168 | res += ")"; 169 | return res; 170 | } 171 | 172 | // vector deque list set map 173 | // template 174 | template::value, typename T_container::value_type>::type> 175 | string to_string(T_container any) { 176 | string res = ""; 177 | res += "{"; 178 | bool first = true; 179 | for (auto x : any) { 180 | if (!first) res += ", "; 181 | first = false; 182 | res += to_string(x); 183 | } 184 | res += "}"; 185 | return res; 186 | } 187 | 188 | // ------------------------------------------------------------------------- 189 | 190 | #define LINE_NO cerr << __LINE__ << "\t|" 191 | #define exit(x) cerr << "Time elapsed : " << fixed << setprecision(3) << 1.0 * clock() / (CLOCKS_PER_SEC * 60.0) << " min.\n"; \ 192 | exit(x) 193 | 194 | void adjust_spaces(string& s) { 195 | if(s.empty()) return; 196 | if(s.front() != ' ') s = ' ' + s; 197 | if(s.back() != ' ') s += ' '; 198 | } 199 | 200 | #define dbg(...) LINE_NO, _debug1(#__VA_ARGS__), _debug2(__VA_ARGS__), 0 201 | 202 | vector all_variables{}; 203 | void _debug1(const string &s) 204 | { 205 | if(s.empty()) return; 206 | string variable = ""; 207 | int bracket = 0, start = all_variables.size(); 208 | for(char c : s) 209 | { 210 | if (c == '(') bracket++; 211 | else if (c == ')') bracket--; 212 | else if (c == ',' and bracket == 0) { 213 | adjust_spaces(variable); 214 | all_variables.emplace_back(variable); 215 | variable = ""; 216 | continue; 217 | } 218 | variable += c; 219 | } 220 | adjust_spaces(variable); 221 | all_variables.emplace_back(variable); 222 | reverse(all_variables.begin() + start, all_variables.end()); 223 | } 224 | 225 | void _debug2() { 226 | cerr << '\n'; 227 | } 228 | template 229 | void _debug2(Head H) { 230 | cerr << all_variables.back() << "= " << to_string(H) << '\n'; 231 | all_variables.pop_back(); 232 | } 233 | template 234 | void _debug2(Head H, Tail ... T) { 235 | cerr << all_variables.back() << "= " << to_string(H) << ","; 236 | all_variables.pop_back(); 237 | _debug2(T...); 238 | } 239 | 240 | // #define d(...) LINE_NO << " [" << #__VA_ARGS__ << "] : ", _debug(__VA_ARGS__) 241 | // void _debug() { 242 | // cerr << '\n'; 243 | // } 244 | // template 245 | // void _debug(Head H, Tail ... T) { 246 | // cerr << to_string(H) << " "; 247 | // _debug(T...); 248 | // } 249 | 250 | #endif -------------------------------------------------------------------------------- /Programming/default.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | #ifdef DHIRAJ 5 | #include "D:/dhiraj/Programming/debug.h" 6 | #else 7 | #define dbg(...) 1 8 | #define cerr if(0) cerr 9 | #endif 10 | 11 | using ll = long long int; 12 | #define endl '\n' 13 | 14 | template 15 | inline istream & operator >> (istream &in, pair &p) { 16 | in >> p.first >> p.second; 17 | return in; 18 | } 19 | template 20 | inline istream & operator >> (istream &in, vector &v) { 21 | for(T &x : v) in >> x; 22 | return in; 23 | } 24 | 25 | void solve(ll &); 26 | 27 | int main() 28 | { 29 | #ifdef DHIRAJ 30 | freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); 31 | freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); 32 | freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); 33 | #endif 34 | 35 | ios::sync_with_stdio(0); 36 | cin.tie(0); 37 | 38 | ll tc = 1; 39 | solve(tc); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /Programming/diff.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dhiraj-01/CP/adf0286287830a63aa8797fd47a6c8cee4acc1b4/Programming/diff.txt -------------------------------------------------------------------------------- /Programming/e1.txt: -------------------------------------------------------------------------------- 1 | Case #1 2 | Case #2 3 | Case #3 4 | Case #4 5 | Case #5 6 | Case #6 7 | Case #7 8 | Case #8 9 | Case #9 10 | Case #10 11 | 49 | 12 | -------------------------------------------------------------------------------- /Programming/i1.txt: -------------------------------------------------------------------------------- 1 | 10 -------------------------------------------------------------------------------- /Programming/o1.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | Hello World 3 | Hello World 4 | Hello World 5 | Hello World 6 | Hello World 7 | Hello World 8 | Hello World 9 | Hello World 10 | Hello World 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Competitive Programming 2 | 3 | - https://workat.tech/resources 4 | 5 | ### Practice 6 | - Pick a language 7 | - Learn topic 8 | - Solve standard problems 9 | - Try problems from online sites (codeforces, atcoder, leetcode, codechef ...) 10 | - Participate in the contest 11 | - Practice more (: 12 | 13 | ### 📚 Book 14 | https://cses.fi/book/book.pdf 15 | 16 | ### Resources 17 | - https://leetcode.com/ **The Best for Coding Interviews** 18 | - https://usaco.guide/ 19 | - https://cp-algorithms.com/ 20 | 21 | ### 🌐 Coding websites 22 | **CP** 23 | - [Codeforces](https://codeforces.com/) 24 | - [Atcoder](https://atcoder.jp/) 25 | - [Codechef](https://www.codechef.com/) 26 | - [Spoj](https://www.spoj.com/) 27 | - [HackerRank](https://www.hackerrank.com/) 28 | - [HackerEarth](https://www.hackerearth.com/) 29 | - [Cses](https://cses.fi/problemset/) 30 | 31 | **Coding Interview** 32 | - [Leetcode](https://leetcode.com/) 33 | - [Binaryseach](https://binarysearch.com/) 34 | 35 | ### My [Sublime text](https://www.sublimetext.com/) setup 36 | **Windows** 37 | https://github.com/Dhiraj-01/CP/blob/main/Sublime%20Text/README.md 38 | 39 | **MacOS** 40 | - Install gcc from homebrew using brew install gcc 41 | - Copy [stdc++.h file](https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-v3/include/precompiled/stdc%2B%2B.h) 42 | - Put above stdc++.h named file at `/Library/Developer/CommandLineTools/usr/include/bits/` 43 | - Sublime c++17 build settings https://pastebin.com/GnNN8cdL 44 | 45 | ### 📹 Youtube 46 | | Name | Link | 47 | | ------------- | -------------------------------------------------------- | 48 | | Striver | https://www.youtube.com/channel/UCJskGeByzRRSvmOyZOz61ig | 49 | | CodeNCode | https://www.youtube.com/channel/UC0zvY3yIBQTrSutsV-4yscQ | 50 | | Errichto | https://www.youtube.com/channel/UCBr_Fu6q9iHYQCh13jmpbrg | 51 | 52 | ### ✌🏻 Other Good Repo. 53 | | Name | Link | 54 | | --------------------- | ------------------------------------------------------------- | 55 | | Tourist 🔥 | https://github.com/the-tourist/algo | 56 | | Atcoder library | https://github.com/atcoder/ac-library | 57 | | Leetcode questions | https://github.com/fterh/leetcode-curation-topical | 58 | | Algo-lib | https://github.com/saketh-are/algo-lib | 59 | | Coding notes | https://github.com/ankitpriyarup/Coding_Notes | 60 | | Alexandru Valeanu | https://github.com/AlexandruValeanu/Competitive-Programming | 61 | | Ashish Gupta | https://github.com/Ashishgup1/Competitive-Coding | 62 | -------------------------------------------------------------------------------- /Scripts/README.md: -------------------------------------------------------------------------------- 1 | ## some useful script 2 | 3 | - **kill.bat** : kill any process 4 | - **diff.py** : diffrence between two files 5 | - **run.py** : run any language code (C, C++, Python, Java) 6 | - **Tip** : you can add path of `Script` folder to environment variable and access it from anywhere :) -------------------------------------------------------------------------------- /Scripts/diff.py: -------------------------------------------------------------------------------- 1 | # compare two files 2 | 3 | import sys 4 | from os import system 5 | 6 | # default file 7 | file1 = "D:/dhiraj/Programming/o1.txt" 8 | file2 = "D:/dhiraj/Programming/o2.txt" 9 | 10 | if len(sys.argv) >= 3: 11 | file1 = sys.argv[1] 12 | file2 = sys.argv[2] 13 | 14 | with open(file1, "r") as O1: 15 | o1_lines = O1.readlines() 16 | 17 | with open(file2, "r") as O2: 18 | o2_lines = O2.readlines() 19 | 20 | for i in range(len(o1_lines)): 21 | o1_lines[i] = o1_lines[i].strip() 22 | 23 | for i in range(len(o2_lines)): 24 | o2_lines[i] = o2_lines[i].strip() 25 | 26 | # remove extra empty lines from end 27 | l1 = len(o1_lines) - 1 28 | while(l1 >= 0 and o1_lines[l1] == ""): 29 | o1_lines.pop(l1) 30 | l1 -= 1 31 | 32 | l2 = len(o2_lines) - 1 33 | while(l2 >= 0 and o2_lines[l2] == ""): 34 | o2_lines.pop(l2) 35 | l2 -= 1 36 | 37 | line_no = 0 38 | diffrence = "" 39 | 40 | l1 = len(o1_lines) 41 | l2 = len(o2_lines) 42 | 43 | while line_no < l1 or line_no < l2: 44 | #Read the next line from the file 45 | f1_line = f2_line = '' 46 | if(line_no < l1): 47 | f1_line = o1_lines[line_no] 48 | if(line_no < l2): 49 | f2_line = o2_lines[line_no] 50 | 51 | #Increment line counter 52 | line_no += 1 53 | 54 | # Compare the lines from both file 55 | if f1_line != f2_line: 56 | # If a line does not exist on file2 then mark the output with + sign 57 | if f2_line == '' and f1_line != '': 58 | diffrence += " -- {}\t{}\n".format(line_no, f1_line) 59 | # otherwise output the line on file1 and mark it with > sign 60 | elif f1_line != '': 61 | diffrence += " - {}\t{}\n".format(line_no, f1_line) 62 | 63 | # If a line does not exist on file1 then mark the output with + sign 64 | if f1_line == '' and f2_line != '': 65 | diffrence += " ++ {}\t{}\n".format(line_no, f2_line) 66 | # otherwise output the line on file2 and mark it with < sign 67 | elif f2_line != '': 68 | diffrence += " + {}\t{}\n".format(line_no, f2_line) 69 | 70 | if(diffrence == ""): 71 | diffrence = "Both files are identical." 72 | print(diffrence) 73 | else: 74 | print(diffrence) 75 | -------------------------------------------------------------------------------- /Scripts/kill.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | if "%*" NEQ "" (taskkill /IM "%*" /F) else ( 3 | echo usage: kill "process_name" 4 | echo ex. kill chrome.exe, kill a.exe 5 | ) -------------------------------------------------------------------------------- /Scripts/run.py: -------------------------------------------------------------------------------- 1 | import sys, subprocess 2 | from os import path 3 | 4 | def Run(cmd): 5 | subprocess.run(cmd, shell = True, check = True) 6 | 7 | def getExe(options): 8 | exe = "a.exe" 9 | for i in range(len(options) - 1): 10 | if options[i] == '-o': 11 | exe = options[i + 1] 12 | del options[i] # remove '-o' 13 | del options[i] # remove 'exe' 14 | break 15 | return exe 16 | 17 | def showHelp(): 18 | # print("please provide file path as an argument.") 19 | print("run ") 20 | print("languages : C, C++, Python or Java.\n") 21 | print("sample examples:") 22 | print("run 1.cpp") 23 | print("run 1.cpp -DDHIRAJ") 24 | print("run 1.py < input.txt") 25 | print("run 1.java < input.txt > output.txt") 26 | print("run 1.cpp -DDHIRAJ < input.txt > output.txt") 27 | 28 | # print(sys.argv) 29 | if(len(sys.argv) == 1): 30 | showHelp() 31 | sys.exit(0) 32 | 33 | try: 34 | file_path = sys.argv[1] 35 | if not path.exists(file_path): 36 | print(file_path, "path of the file is Invalid :(") 37 | sys.exit(0) 38 | 39 | lang = file_path.split('.')[-1] 40 | options = [] 41 | for i in range(2, len(sys.argv)): 42 | options.append(sys.argv[i]) 43 | 44 | cmd = "echo language not supported :(" 45 | 46 | if(lang == "cpp"): 47 | exe = getExe(options) 48 | options = " ".join(options) 49 | cmd = f'g++ -std=c++17 {options} "{file_path}" -o "{exe}" && "{exe}"' 50 | 51 | elif(lang == "c"): 52 | exe = getExe(options) 53 | options = " ".join(options) 54 | cmd = f'gcc {options} "{file_path}" -o "{exe}" && "{exe}"' 55 | 56 | elif(lang == "py"): 57 | options = " ".join(options) 58 | cmd = f'python "{file_path}" {options}' 59 | 60 | elif(lang == "java"): 61 | options = " ".join(options) 62 | cmd = f'javac {options} "{file_path}" && java Main' 63 | 64 | # print(cmd) 65 | Run(cmd) 66 | 67 | except KeyboardInterrupt as e: 68 | sys.exit(0) -------------------------------------------------------------------------------- /Sublime Text/Build/C++.sublime-build: -------------------------------------------------------------------------------- 1 | /* 2 | # Use 3 | - change c++ standard, if c++17 is not avilable 4 | - Sublime => Tools => Build System => New Build System ... 5 | - copy paste this code 6 | - save as C++.sublime-build 7 | - Tools => Build System => C++ 8 | - write some code. 9 | - ctrl + b to run your code. 10 | */ 11 | 12 | { 13 | "cmd" : ["g++", "-std=c++17", "${file_name}", "-o", "a.exe", "&&", "start", "cmd", "/c", "a.exe & echo. & pause"], 14 | // remove "& echo. & pause" to automatically close terminal 15 | // "cmd" : ["g++", "-std=c++17", "${file_name}", "-o", "a.exe", "&&", "start", "cmd", "/c", "a.exe"], 16 | 17 | "shell" : true, 18 | "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", 19 | "working_dir" : "${file_path}", 20 | "selector" : "source.cpp, source.c++", 21 | } 22 | 23 | /* 24 | ${file} -> absolute path "D:/Codes/hello.cpp" 25 | ${file_name} -> just file name "hello.cpp" 26 | ${file_base_name} -> file name without extention "hello" 27 | */ 28 | -------------------------------------------------------------------------------- /Sublime Text/Build/Java.sublime-build: -------------------------------------------------------------------------------- 1 | { 2 | "cmd": ["javac", "${file_name}", "&&","java", "Main"], // $file_base_name 3 | "shell": true, 4 | "file_regex": "^(...*?):([0-9]*):?([0-9]*)", 5 | "selector" : "source.java", 6 | 7 | "variants": [ 8 | { 9 | "name" : "Terminal", 10 | "cmd" : ["javac", "${file_name}", "&&", "start","cmd", "/c", "java Main && pause"], // $file_base_name 11 | "shell": true, 12 | } 13 | ], 14 | } -------------------------------------------------------------------------------- /Sublime Text/Build/Javascript.sublime-build: -------------------------------------------------------------------------------- 1 | { 2 | "shell_cmd" : "node ${file}", 3 | "shell" : true, 4 | "working_dir" : "${file_path}", 5 | "selector" : "source.js", 6 | 7 | "variants": [ 8 | { 9 | "name" : "Terminal", 10 | "shell_cmd" : "start cmd /c \"node ${file} & pause\"", 11 | } 12 | ] 13 | } 14 | -------------------------------------------------------------------------------- /Sublime Text/Build/Php.sublime-build: -------------------------------------------------------------------------------- 1 | { 2 | "cmd": ["php", "$file"], 3 | "file_regex": "php$", 4 | "selector": "source.php", 5 | } -------------------------------------------------------------------------------- /Sublime Text/Build/Python3.sublime-build: -------------------------------------------------------------------------------- 1 | { 2 | "cmd": ["python" ,"${file_name}"], 3 | "shell": true, 4 | "working_dir": "${file_path}", 5 | "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)", 6 | "selector": "source.python, source.py", 7 | 8 | "variants": [ 9 | { 10 | "name": "Terminal", 11 | "cmd": ["start","cmd", "/c", "python ${file_name} && pause"], 12 | } 13 | ], 14 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/IO/basic_ios.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "bad\tfunc", "contents": "bad($1)"}, 6 | {"trigger": "copyfmt\tfunc", "contents": "copyfmt($1)"}, 7 | {"trigger": "eof\tfunc", "contents": "eof($1)"}, 8 | {"trigger": "event\tfunc", "contents": "event($1)"}, 9 | {"trigger": "event_callback\tfunc", "contents": "event_callback($1)"}, 10 | {"trigger": "exceptions\tfunc", "contents": "exceptions($1)"}, 11 | {"trigger": "fail\tfunc", "contents": "fail($1)"}, 12 | {"trigger": "failure\tfunc", "contents": "failure($1)"}, 13 | {"trigger": "flags\tfunc", "contents": "flags($1)"}, 14 | {"trigger": "fmtflags\tfunc", "contents": "fmtflags($1)"}, 15 | {"trigger": "getloc\tfunc", "contents": "getloc($1)"}, 16 | {"trigger": "good\tfunc", "contents": "good($1)"}, 17 | {"trigger": "imbue\tfunc", "contents": "imbue($1)"}, 18 | {"trigger": "Init\tfunc", "contents": "Init($1)"}, 19 | {"trigger": "init\tfunc", "contents": "init($1)"}, 20 | {"trigger": "ios\tfunc", "contents": "ios"}, 21 | {"trigger": "iostate\tfunc", "contents": "iostate($1)"}, 22 | {"trigger": "iword\tfunc", "contents": "iword($1)"}, 23 | {"trigger": "narrow\tfunc", "contents": "narrow($1)"}, 24 | {"trigger": "openmode\tfunc", "contents": "openmode($1)"}, 25 | {"trigger": "precision\tfunc", "contents": "precision($1)"}, 26 | {"trigger": "pword\tfunc", "contents": "pword($1)"}, 27 | {"trigger": "rdbuf\tfunc", "contents": "rdbuf($1)"}, 28 | {"trigger": "rdstate\tfunc", "contents": "rdstate($1)"}, 29 | {"trigger": "register_callback\tfunc", "contents": "register_callback($1)"}, 30 | {"trigger": "seekdir\tfunc", "contents": "seekdir($1)"}, 31 | {"trigger": "setf\tfunc", "contents": "setf($1)"}, 32 | {"trigger": "setstate\tfunc", "contents": "setstate($1)"}, 33 | {"trigger": "sync_with_stdio\tfunc", "contents": "sync_with_stdio($1)"}, 34 | {"trigger": "tie\tfunc", "contents": "tie($1)"}, 35 | {"trigger": "unsetf\tfunc", "contents": "unsetf($1)"}, 36 | {"trigger": "widen\tfunc", "contents": "widen($1)"}, 37 | {"trigger": "width\tfunc", "contents": "width($1)"}, 38 | {"trigger": "wios\tfunc", "contents": "wios"}, 39 | {"trigger": "xalloc\tfunc", "contents": "xalloc($1)"}, 40 | ] 41 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/IO/iomanip.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "get_money\tiomanip", "contents": "get_money"}, 6 | {"trigger": "get_time\tiomanip", "contents": "get_time"}, 7 | {"trigger": "put_money\tiomanip", "contents": "put_money"}, 8 | {"trigger": "put_time\tiomanip", "contents": "put_time"}, 9 | {"trigger": "resetiosflags\tiomanip", "contents": "resetiosflags"}, 10 | {"trigger": "setbase\tiomanip", "contents": "setbase"}, 11 | {"trigger": "setfill\tiomanip", "contents": "setfill"}, 12 | {"trigger": "setiosflags\tiomanip", "contents": "setiosflags"}, 13 | {"trigger": "setprecision\tiomanip", "contents": "setprecision"}, 14 | {"trigger": "setw\tiomanip", "contents": "setw"}, 15 | ] 16 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/IO/ios.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "basic_ios\tios", "contents": "basic_ios"}, 6 | {"trigger": "boolalpha\tios", "contents": "boolalpha"}, 7 | {"trigger": "dec\tios", "contents": "dec"}, 8 | {"trigger": "defaultfloat\tios", "contents": "defaultfloat"}, 9 | {"trigger": "fixed\tios", "contents": "fixed"}, 10 | {"trigger": "fpos\tios", "contents": "fpos"}, 11 | {"trigger": "hex\tios", "contents": "hex"}, 12 | {"trigger": "hexfloat\tios", "contents": "hexfloat"}, 13 | {"trigger": "internal\tios", "contents": "internal"}, 14 | {"trigger": "io_errc\tios", "contents": "io_errc"}, 15 | {"trigger": "ios\tios", "contents": "ios"}, 16 | {"trigger": "ios_base\tios", "contents": "ios_base"}, 17 | {"trigger": "left\tios", "contents": "left"}, 18 | {"trigger": "noboolalpha\tios", "contents": "noboolalpha"}, 19 | {"trigger": "noshowbase\tios", "contents": "noshowbase"}, 20 | {"trigger": "noshowpoint\tios", "contents": "noshowpoint"}, 21 | {"trigger": "noshowpos\tios", "contents": "noshowpos"}, 22 | {"trigger": "noskipws\tios", "contents": "noskipws"}, 23 | {"trigger": "nounitbuf\tios", "contents": "nounitbuf"}, 24 | {"trigger": "nouppercase\tios", "contents": "nouppercase"}, 25 | {"trigger": "oct\tios", "contents": "oct"}, 26 | {"trigger": "right\tios", "contents": "right"}, 27 | {"trigger": "scientific\tios", "contents": "scientific"}, 28 | {"trigger": "showbase\tios", "contents": "showbase"}, 29 | {"trigger": "showpoint\tios", "contents": "showpoint"}, 30 | {"trigger": "showpos\tios", "contents": "showpos"}, 31 | {"trigger": "skipws\tios", "contents": "skipws"}, 32 | {"trigger": "streamoff\tios", "contents": "streamoff"}, 33 | {"trigger": "streampos\tios", "contents": "streampos"}, 34 | {"trigger": "streamsize\tios", "contents": "streamsize"}, 35 | {"trigger": "unitbuf\tios", "contents": "unitbuf"}, 36 | {"trigger": "wios\tios", "contents": "wios"}, 37 | {"trigger": "wstreampos\tios", "contents": "wstreampos"}, 38 | ] 39 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/IO/iostream.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | 6 | {"trigger": "cin\tiostream", "contents": "cin >> "}, 7 | {"trigger": "cout\tiostream", "contents": "cout << "}, 8 | {"trigger": "cerr\tiostream", "contents": "cerr << "}, 9 | 10 | // {"trigger": "cerr\tiostream", "contents": "${1:std::}cerr"}, 11 | // {"trigger": "cin\tiostream", "contents": "${1:std::}cin"}, 12 | // {"trigger": "cout\tiostream", "contents": "${1:std::}cout"}, 13 | 14 | {"trigger": "clog\tiostream", "contents": "${1:std::}clog"}, 15 | {"trigger": "wcerr\tiostream", "contents": "${1:std::}wcerr"}, 16 | {"trigger": "wcin\tiostream", "contents": "${1:std::}wcin"}, 17 | {"trigger": "wclog\tiostream", "contents": "${1:std::}wclog"}, 18 | {"trigger": "wcout\tiostream", "contents": "${1:std::}wcout"}, 19 | ] 20 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/IO/ostream.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | // {"trigger": "endl\tostream", "contents": "${1:std::}endl;"}, 6 | // {"trigger": "endl\tostream", "contents": "\"\\n\";"}, 7 | {"trigger": "ends\tostream", "contents": "${1:std::}ends;"}, 8 | {"trigger": "flush\tostream", "contents": "${1:std::}flush;"}, 9 | ] 10 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/IO/streambuf.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "eback\tstreambuf", "contents": "${1:streambuf::}eback($2)"}, 6 | {"trigger": "egptr\tstreambuf", "contents": "${1:streambuf::}egptr($2)"}, 7 | {"trigger": "epptr\tstreambuf", "contents": "${1:streambuf::}epptr($2)"}, 8 | {"trigger": "gbump\tstreambuf", "contents": "${1:streambuf::}gbump($2)"}, 9 | {"trigger": "getloc\tstreambuf", "contents": "${1:streambuf::}getloc($2)"}, 10 | {"trigger": "gptr\tstreambuf", "contents": "${1:streambuf::}gptr($2)"}, 11 | {"trigger": "imbue\tstreambuf", "contents": "${1:streambuf::}imbue($2)"}, 12 | {"trigger": "in_avail\tstreambuf", "contents": "${1:streambuf::}in_avail($2)"}, 13 | {"trigger": "overflow\tstreambuf", "contents": "${1:streambuf::}overflow($2)"}, 14 | {"trigger": "pbackfail\tstreambuf", "contents": "${1:streambuf::}pbackfail($2)"}, 15 | {"trigger": "pbase\tstreambuf", "contents": "${1:streambuf::}pbase($2)"}, 16 | {"trigger": "pbump\tstreambuf", "contents": "${1:streambuf::}pbump($2)"}, 17 | {"trigger": "pptr\tstreambuf", "contents": "${1:streambuf::}pptr($2)"}, 18 | {"trigger": "pubimbue\tstreambuf", "contents": "${1:streambuf::}pubimbue($2)"}, 19 | {"trigger": "pubseekoff\tstreambuf", "contents": "${1:streambuf::}pubseekoff($2)"}, 20 | {"trigger": "pubseekpos\tstreambuf", "contents": "${1:streambuf::}pubseekpos($2)"}, 21 | {"trigger": "pubsetbuf\tstreambuf", "contents": "${1:streambuf::}pubsetbuf($2)"}, 22 | {"trigger": "pubsync\tstreambuf", "contents": "${1:streambuf::}pubsync($2)"}, 23 | {"trigger": "sbumpc\tstreambuf", "contents": "${1:streambuf::}sbumpc($2)"}, 24 | {"trigger": "seekoff\tstreambuf", "contents": "${1:streambuf::}seekoff($2)"}, 25 | {"trigger": "seekpos\tstreambuf", "contents": "${1:streambuf::}seekpos($2)"}, 26 | {"trigger": "setbuf\tstreambuf", "contents": "${1:streambuf::}setbuf($2)"}, 27 | {"trigger": "setg\tstreambuf", "contents": "${1:streambuf::}setg($2)"}, 28 | {"trigger": "setp\tstreambuf", "contents": "${1:streambuf::}setp($2)"}, 29 | {"trigger": "sgetc\tstreambuf", "contents": "${1:streambuf::}sgetc($2)"}, 30 | {"trigger": "sgetn\tstreambuf", "contents": "${1:streambuf::}sgetn($2)"}, 31 | {"trigger": "showmanyc\tstreambuf", "contents": "${1:streambuf::}showmanyc($2)"}, 32 | {"trigger": "snextc\tstreambuf", "contents": "${1:streambuf::}snextc($2)"}, 33 | {"trigger": "sputbackc\tstreambuf", "contents": "${1:streambuf::}sputbackc($2)"}, 34 | {"trigger": "sputc\tstreambuf", "contents": "${1:streambuf::}sputc($2)"}, 35 | {"trigger": "sputn\tstreambuf", "contents": "${1:streambuf::}sputn($2)"}, 36 | {"trigger": "sungetc\tstreambuf", "contents": "${1:streambuf::}sungetc($2)"}, 37 | {"trigger": "swap\tstreambuf", "contents": "${1:streambuf::}swap($2)"}, 38 | {"trigger": "sync\tstreambuf", "contents": "${1:streambuf::}sync($2)"}, 39 | {"trigger": "uflow\tstreambuf", "contents": "${1:streambuf::}uflow($2)"}, 40 | {"trigger": "underflow\tstreambuf", "contents": "${1:streambuf::}underflow($2)"}, 41 | {"trigger": "xsgetn\tstreambuf", "contents": "${1:streambuf::}xsgetn($2)"}, 42 | {"trigger": "xsputn\tstreambuf", "contents": "${1:streambuf::}xsputn($2)"}, 43 | ] 44 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/classes/class.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Class 3 | 11 | class_ 12 | source.c++, source.objc++ 13 | 14 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/classes/class_template.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Class Template 3 | 5 | class ${2:${TM_FILENAME/(.+)\..+|.*/$1/:name}} { 6 | private: 7 | ${3:/* data */} 8 | public: 9 | ${2/(\w+).*/$1/}(${4:/* args */}) ${5:= default;} 10 | }; 11 | ]]> 12 | classtemp 13 | source.c++, source.objc++ 14 | 15 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/classes/exception.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Create exception 3 | 9 | excep 10 | source.c++, source.objc++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/classes/struct.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Struct 3 | 8 | struct_ 9 | source.c, source.objc, source.c++, source.objc++ 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/classes/struct_template.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Templated Struct 3 | 5 | struct ${2:${TM_FILENAME/(.+)\..+|.*/$1/:name}} { 6 | ${0:/* code */} 7 | }; 8 | ]]> 9 | structtemp 10 | source.c++, source.objc++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/constexpr_function.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Constexpr Function 3 | 8 | confunct 9 | source.c++ 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/main_function.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | main(…) 3 | 9 | main_ 10 | source.c, source.objc, source.c++, source.objc++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/regular_function.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Regular Function 3 | 8 | funct 9 | source.c, source.objc, source.c++, source.objc++ 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/templated_function.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Templated Function 3 | 5 | ${2:int} ${3:function_name}(${4:/* args */}) noexcept { 6 | $0 7 | } 8 | ]]> 9 | tempfunc 10 | source.c++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/templated_function_auto.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Template Auto Function 3 | 5 | auto ${2:function_name}(${3:/* args */}) -> decltype(${4:/* expression */}) { 6 | ${0:/* code */} 7 | } 8 | ]]> 9 | tempfunca 10 | source.c++, source.objc++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/templated_function_auto_constexpr.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Template Auto Constexpr Function 3 | 5 | constexpr auto ${2:function_name}(${3:/* args */}) noexcept -> decltype(${4:/* code */}) { 6 | return ${4:/* code */}; 7 | } 8 | ]]> 9 | tempcfunca 10 | source.c++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functions/templated_function_constexpr.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Template Constexpr Function 3 | 5 | constexpr ${2:int} ${3:function_name}(${4:/* args */}) { 6 | return ${5:/* code */}; 7 | } 8 | ]]> 9 | tempcfunc 10 | source.c++ 11 | 12 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functors/binary_template_functor.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Binary Template Functor 3 | 6 | constexpr auto operator()(T&& t, U&& u) const noexcept -> decltype(${2:/* code */}) { 7 | return ${2:/* code */}; 8 | } 9 | }; 10 | ]]> 11 | bintempfunctor 12 | source.c++ 13 | 14 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functors/functor.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Functor 3 | 10 | functor 11 | source.c++ 12 | 13 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/functors/unary_template_functor.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Unary Template Functor 3 | 6 | constexpr auto operator()(T&& t) const noexcept -> decltype(${2:/* code */}) { 7 | return ${2:/* code */}; 8 | } 9 | }; 10 | ]]> 11 | tempfunctor 12 | source.c++ 13 | 14 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/headers/Cheader.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c", 3 | "completions": 4 | [ 5 | {"trigger": "I-assert\tinc", "contents": "#include \n"}, 6 | {"trigger": "I-ctype\tinc", "contents": "#include \n"}, 7 | {"trigger": "I-errno\tinc", "contents": "#include \n"}, 8 | {"trigger": "I-fenv\tinc", "contents": "#include \n"}, 9 | {"trigger": "I-float\tinc", "contents": "#include \n"}, 10 | {"trigger": "I-inttypes\tinc", "contents": "#include \n"}, 11 | {"trigger": "I-iso646\tinc", "contents": "#include \n"}, 12 | {"trigger": "I-limits\tinc", "contents": "#include \n"}, 13 | {"trigger": "I-locale\tinc", "contents": "#include \n"}, 14 | {"trigger": "I-math\tinc", "contents": "#include \n"}, 15 | {"trigger": "I-setjmp\tinc", "contents": "#include \n"}, 16 | {"trigger": "I-signal\tinc", "contents": "#include \n"}, 17 | {"trigger": "I-stdarg\tinc", "contents": "#include \n"}, 18 | {"trigger": "I-stdbool\tinc", "contents": "#include \n"}, 19 | {"trigger": "I-stddef\tinc", "contents": "#include \n"}, 20 | {"trigger": "I-stdint\tinc", "contents": "#include \n"}, 21 | {"trigger": "I-stdio\tinc", "contents": "#include \n"}, 22 | {"trigger": "I-stdlib\tinc", "contents": "#include \n"}, 23 | {"trigger": "I-string\tinc", "contents": "#include \n"}, 24 | {"trigger": "I-tgmath\tinc", "contents": "#include \n"}, 25 | {"trigger": "I-time\tinc", "contents": "#include \n"}, 26 | {"trigger": "I-uchar\tinc", "contents": "#include \n"}, 27 | {"trigger": "I-wchar\tinc", "contents": "#include \n"}, 28 | {"trigger": "I-wctype\tinc", "contents": "#include \n"} 29 | ] 30 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/headers/CppHeader.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "I-stdc\tbits/stdc++.h", "contents": "#include \n"}, 6 | {"trigger": "I-algorithm\tinc", "contents": "#include \n"}, 7 | {"trigger": "I-array\tinc", "contents": "#include \n"}, 8 | {"trigger": "I-atomic\tinc", "contents": "#include \n"}, 9 | {"trigger": "I-bitset\tinc", "contents": "#include \n"}, 10 | {"trigger": "I-chrono\tinc", "contents": "#include \n"}, 11 | {"trigger": "I-codecvt\tinc", "contents": "#include \n"}, 12 | {"trigger": "I-complex\tinc", "contents": "#include \n"}, 13 | {"trigger": "I-condition_variable\tinc", "contents": "#include \n"}, 14 | {"trigger": "I-deque\tinc", "contents": "#include \n"}, 15 | {"trigger": "I-exception\tinc", "contents": "#include \n"}, 16 | {"trigger": "I-forward_list\tinc", "contents": "#include \n"}, 17 | {"trigger": "I-fstream\tinc", "contents": "#include \n"}, 18 | {"trigger": "I-functional\tinc", "contents": "#include \n"}, 19 | {"trigger": "I-future\tinc", "contents": "#include \n"}, 20 | {"trigger": "I-initializer_list\tinc", "contents": "#include \n"}, 21 | {"trigger": "I-iomanip\tinc", "contents": "#include \n"}, 22 | {"trigger": "I-ios\tinc", "contents": "#include \n"}, 23 | {"trigger": "I-iosfwd\tinc", "contents": "#include \n"}, 24 | {"trigger": "I-iostream\tinc", "contents": "#include \n"}, 25 | {"trigger": "I-istream\tinc", "contents": "#include \n"}, 26 | {"trigger": "I-iterator\tinc", "contents": "#include \n"}, 27 | {"trigger": "I-limits\tinc", "contents": "#include \n"}, 28 | {"trigger": "I-list\tinc", "contents": "#include \n"}, 29 | {"trigger": "I-locale\tinc", "contents": "#include \n"}, 30 | {"trigger": "I-map\tinc", "contents": "#include \n"}, 31 | {"trigger": "I-memory\tinc", "contents": "#include \n"}, 32 | {"trigger": "I-mutex\tinc", "contents": "#include \n"}, 33 | {"trigger": "I-new\tinc", "contents": "#include \n"}, 34 | {"trigger": "I-numeric\tinc", "contents": "#include \n"}, 35 | {"trigger": "I-ostream\tinc", "contents": "#include \n"}, 36 | {"trigger": "I-queue\tinc", "contents": "#include \n"}, 37 | {"trigger": "I-random\tinc", "contents": "#include \n"}, 38 | {"trigger": "I-ratio\tinc", "contents": "#include \n"}, 39 | {"trigger": "I-regex\tinc", "contents": "#include \n"}, 40 | {"trigger": "I-set\tinc", "contents": "#include \n"}, 41 | {"trigger": "I-sstream\tinc", "contents": "#include \n"}, 42 | {"trigger": "I-stack\tinc", "contents": "#include \n"}, 43 | {"trigger": "I-stdexcept\tinc", "contents": "#include \n"}, 44 | {"trigger": "I-streambuf\tinc", "contents": "#include \n"}, 45 | {"trigger": "I-string\tinc", "contents": "#include \n"}, 46 | {"trigger": "I-system_error\tinc", "contents": "#include \n"}, 47 | {"trigger": "I-thread\tinc", "contents": "#include \n"}, 48 | {"trigger": "I-tuple\tinc", "contents": "#include \n"}, 49 | {"trigger": "I-type_traits\tinc", "contents": "#include \n"}, 50 | {"trigger": "I-typeindex\tinc", "contents": "#include \n"}, 51 | {"trigger": "I-typeinfo\tinc", "contents": "#include \n"}, 52 | {"trigger": "I-unordered_map\tinc", "contents": "#include \n"}, 53 | {"trigger": "I-unordered_set\tinc", "contents": "#include \n"}, 54 | {"trigger": "I-utility\tinc", "contents": "#include \n"}, 55 | {"trigger": "I-valarray\tinc", "contents": "#include \n"}, 56 | {"trigger": "I-vector\tinc", "contents": "#include \n"}, 57 | ] 58 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/install.txt: -------------------------------------------------------------------------------- 1 | https://github.com/Rapptz/cpp-sublime-snippet 2 | https://github.com/tushortz/CPP-Completions -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/lambda/catchallbyref.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Lambda – Catch All by Reference 3 | 8 | lamref 9 | source.c++, source.c++11 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/lambda/catchallbyvalue.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Lambda – Catch All by Value 3 | 8 | lamval 9 | source.c++, source.c++11 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/lambda/full.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Lambda – Full Spec 3 | ${5:/* return */} { 5 | ${6:/* code */} 6 | } 7 | ]]> 8 | lamfull 9 | source.c++, source.c++11 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/lambda/lamret.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Lambda – Explicit Return Declaration 3 | ${3:/* return */} { 5 | ${4:/* code */} 6 | } 7 | ]]> 8 | lamret 9 | source.c++, source.c++11 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/lambda/minimal.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Lambda – Minimal 3 | 8 | lammin 9 | source.c++, source.c++11 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/methods/methods.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "adjacent_find\tfunc", "contents": "adjacent_find($1)"}, 6 | {"trigger": "accumulate\tfunc", "contents": "accumulate(${1:}, 0LL)"}, 7 | {"trigger": "advance\tfunc", "contents": "advance($1)"}, 8 | {"trigger": "all_of\tfunc", "contents": "all_of($1)"}, 9 | {"trigger": "any_of\tfunc", "contents": "any_of($1)"}, 10 | {"trigger": "assign\tfunc", "contents": "assign($1)"}, 11 | {"trigger": "at\tfunc", "contents": "at($1)"}, 12 | {"trigger": "back\tfunc", "contents": "back($1)"}, 13 | {"trigger": "back_inserter\tfunc", "contents": "back_inserter($1)"}, 14 | {"trigger": "before_begin\tfunc", "contents": "before_begin($1)"}, 15 | {"trigger": "begin\tfunc", "contents": "begin($1)"}, 16 | {"trigger": "binary_search\tfunc", "contents": "binary_search($1)"}, 17 | {"trigger": "bucket\tfunc", "contents": "bucket($1)"}, 18 | {"trigger": "bucket_count\tfunc", "contents": "bucket_count($1)"}, 19 | {"trigger": "bucket_size\tfunc", "contents": "bucket_size($1)"}, 20 | {"trigger": "capacity\tfunc", "contents": "capacity($1)"}, 21 | {"trigger": "cbefore_begin\tfunc", "contents": "cbefore_begin($1)"}, 22 | {"trigger": "cbegin\tfunc", "contents": "cbegin($1)"}, 23 | {"trigger": "cend\tfunc", "contents": "cend($1)"}, 24 | {"trigger": "clear\tfunc", "contents": "clear($1)"}, 25 | {"trigger": "close\tfunc", "contents": "close($1)"}, 26 | {"trigger": "copy\tfunc", "contents": "copy($1)"}, 27 | {"trigger": "copy_backward\tfunc", "contents": "copy_backward($1)"}, 28 | {"trigger": "copy_if\tfunc", "contents": "copy_if($1)"}, 29 | {"trigger": "copy_n\tfunc", "contents": "copy_n($1)"}, 30 | {"trigger": "count\tfunc", "contents": "count($1)"}, 31 | {"trigger": "count_if\tfunc", "contents": "count_if($1)"}, 32 | {"trigger": "crbegin\tfunc", "contents": "crbegin($1)"}, 33 | {"trigger": "crend\tfunc", "contents": "crend($1)"}, 34 | {"trigger": "data\tfunc", "contents": "data($1)"}, 35 | {"trigger": "distance\tfunc", "contents": "distance($1)"}, 36 | {"trigger": "emplace\tfunc", "contents": "emplace($1)"}, 37 | {"trigger": "emplace_after\tfunc", "contents": "emplace_after($1)"}, 38 | {"trigger": "emplace_back\tfunc", "contents": "emplace_back($1)"}, 39 | {"trigger": "emplace_front\tfunc", "contents": "emplace_front($1)"}, 40 | {"trigger": "emplace_hint\tfunc", "contents": "emplace_hint($1)"}, 41 | {"trigger": "empty\tfunc", "contents": "empty($1)"}, 42 | {"trigger": "end\tfunc", "contents": "end($1)"}, 43 | {"trigger": "equal\tfunc", "contents": "equal($1)"}, 44 | {"trigger": "equal_range\tfunc", "contents": "equal_range($1)"}, 45 | {"trigger": "erase\tfunc", "contents": "erase($1)"}, 46 | {"trigger": "erase_after\tfunc", "contents": "erase_after($1)"}, 47 | {"trigger": "fill\tfunc", "contents": "fill($1)"}, 48 | {"trigger": "fill_n\tfunc", "contents": "fill_n($1)"}, 49 | {"trigger": "find\tfunc", "contents": "find($1)"}, 50 | {"trigger": "find_end\tfunc", "contents": "find_end($1)"}, 51 | {"trigger": "find_first_of\tfunc", "contents": "find_first_of($1)"}, 52 | {"trigger": "find_if\tfunc", "contents": "find_if($1)"}, 53 | {"trigger": "find_if_not\tfunc", "contents": "find_if_not($1)"}, 54 | {"trigger": "for_each\tfunc", "contents": "for_each($1)"}, 55 | {"trigger": "front\tfunc", "contents": "front($1)"}, 56 | {"trigger": "front_inserter\tfunc", "contents": "front_inserter($1)"}, 57 | {"trigger": "generate\tfunc", "contents": "generate($1)"}, 58 | {"trigger": "generate_n\tfunc", "contents": "generate_n($1)"}, 59 | {"trigger": "get_allocator\tfunc", "contents": "get_allocator($1)"}, 60 | {"trigger": "hash_function\tfunc", "contents": "hash_function($1)"}, 61 | {"trigger": "includes\tfunc", "contents": "includes($1)"}, 62 | {"trigger": "inplace_merge\tfunc", "contents": "inplace_merge($1)"}, 63 | {"trigger": "insert\tfunc", "contents": "insert($1)"}, 64 | {"trigger": "insert_after\tfunc", "contents": "insert_after($1)"}, 65 | {"trigger": "inserter\tfunc", "contents": "inserter($1)"}, 66 | {"trigger": "is_heap\tfunc", "contents": "is_heap($1)"}, 67 | {"trigger": "is_heap_until\tfunc", "contents": "is_heap_until($1)"}, 68 | {"trigger": "is_open\tfunc", "contents": "is_open($1)"}, 69 | {"trigger": "is_partitioned\tfunc", "contents": "is_partitioned($1)"}, 70 | {"trigger": "is_permutation\tfunc", "contents": "is_permutation($1)"}, 71 | {"trigger": "is_sorted\tfunc", "contents": "is_sorted($1)"}, 72 | {"trigger": "is_sorted_until\tfunc", "contents": "is_sorted_until($1)"}, 73 | {"trigger": "iter_swap\tfunc", "contents": "iter_swap($1)"}, 74 | {"trigger": "key_comp\tfunc", "contents": "key_comp($1)"}, 75 | {"trigger": "key_eq\tfunc", "contents": "key_eq($1)"}, 76 | {"trigger": "lexicographical_compare\tfunc", "contents": "lexicographical_compare($1)"}, 77 | {"trigger": "load_factor\tfunc", "contents": "load_factor($1)"}, 78 | {"trigger": "lower_bound\tfunc", "contents": "lower_bound($1)"}, 79 | {"trigger": "make_heap\tfunc", "contents": "make_heap($1)"}, 80 | {"trigger": "make_move_iterator\tfunc", "contents": "make_move_iterator($1)"}, 81 | {"trigger": "max\tfunc", "contents": "max($1)"}, 82 | {"trigger": "max_bucket_count\tfunc", "contents": "max_bucket_count($1)"}, 83 | {"trigger": "max_element\tfunc", "contents": "max_element($1)"}, 84 | {"trigger": "max_load_factor\tfunc", "contents": "max_load_factor($1)"}, 85 | {"trigger": "max_size\tfunc", "contents": "max_size($1)"}, 86 | {"trigger": "merge\tfunc", "contents": "merge($1)"}, 87 | {"trigger": "min\tfunc", "contents": "min($1)"}, 88 | {"trigger": "min_element\tfunc", "contents": "min_element($1)"}, 89 | {"trigger": "minmax\tfunc", "contents": "minmax($1)"}, 90 | {"trigger": "minmax_element\tfunc", "contents": "minmax_element($1)"}, 91 | {"trigger": "mismatch\tfunc", "contents": "mismatch($1)"}, 92 | {"trigger": "move\tfunc", "contents": "move($1)"}, 93 | {"trigger": "move_backward\tfunc", "contents": "move_backward($1)"}, 94 | {"trigger": "next\tfunc", "contents": "next($1)"}, 95 | {"trigger": "next_permutation\tfunc", "contents": "next_permutation($1)"}, 96 | {"trigger": "none_of\tfunc", "contents": "none_of($1)"}, 97 | {"trigger": "nth_element\tfunc", "contents": "nth_element($1)"}, 98 | {"trigger": "open\tfunc", "contents": "open($1)"}, 99 | {"trigger": "operator\tfunc", "contents": "operator($1)"}, 100 | {"trigger": "operator=\tfunc", "contents": "operator=($1)"}, 101 | {"trigger": "operator[]\tfunc", "contents": "operator[]($1)"}, 102 | {"trigger": "partial_sort\tfunc", "contents": "partial_sort($1)"}, 103 | {"trigger": "partial_sort_copy\tfunc", "contents": "partial_sort_copy($1)"}, 104 | {"trigger": "partition\tfunc", "contents": "partition($1)"}, 105 | {"trigger": "partition_copy\tfunc", "contents": "partition_copy($1)"}, 106 | {"trigger": "partition_point\tfunc", "contents": "partition_point($1)"}, 107 | {"trigger": "pop\tfunc", "contents": "pop($1)"}, 108 | {"trigger": "pop_back\tfunc", "contents": "pop_back($1)"}, 109 | {"trigger": "pop_front\tfunc", "contents": "pop_front($1)"}, 110 | {"trigger": "pop_heap\tfunc", "contents": "pop_heap($1)"}, 111 | {"trigger": "prev\tfunc", "contents": "prev($1)"}, 112 | {"trigger": "prev_permutation\tfunc", "contents": "prev_permutation($1)"}, 113 | {"trigger": "push\tfunc", "contents": "push($1)"}, 114 | {"trigger": "push_back\tfunc", "contents": "push_back($1)"}, 115 | {"trigger": "push_front\tfunc", "contents": "push_front($1)"}, 116 | {"trigger": "push_heap\tfunc", "contents": "push_heap($1)"}, 117 | {"trigger": "random_shuffle\tfunc", "contents": "random_shuffle($1)"}, 118 | {"trigger": "rbegin\tfunc", "contents": "rbegin($1)"}, 119 | {"trigger": "regex_match\tfunc", "contents": "regex_match($1)"}, 120 | {"trigger": "regex_replace\tfunc", "contents": "regex_replace($1)"}, 121 | {"trigger": "regex_search\tfunc", "contents": "regex_search($1)"}, 122 | {"trigger": "rehash\tfunc", "contents": "rehash($1)"}, 123 | {"trigger": "remove\tfunc", "contents": "remove($1)"}, 124 | {"trigger": "remove_copy\tfunc", "contents": "remove_copy($1)"}, 125 | {"trigger": "remove_copy_if\tfunc", "contents": "remove_copy_if($1)"}, 126 | {"trigger": "remove_if\tfunc", "contents": "remove_if($1)"}, 127 | {"trigger": "rend\tfunc", "contents": "rend($1)"}, 128 | {"trigger": "replace\tfunc", "contents": "replace($1)"}, 129 | {"trigger": "replace_copy\tfunc", "contents": "replace_copy($1)"}, 130 | {"trigger": "replace_copy_if\tfunc", "contents": "replace_copy_if($1)"}, 131 | {"trigger": "replace_if\tfunc", "contents": "replace_if($1)"}, 132 | {"trigger": "reserve\tfunc", "contents": "reserve($1)"}, 133 | {"trigger": "resize\tfunc", "contents": "resize($1)"}, 134 | {"trigger": "reverse\tfunc", "contents": "reverse($1)"}, 135 | {"trigger": "reverse_copy\tfunc", "contents": "reverse_copy($1)"}, 136 | {"trigger": "rotate\tfunc", "contents": "rotate($1)"}, 137 | {"trigger": "rotate_copy\tfunc", "contents": "rotate_copy($1)"}, 138 | {"trigger": "search\tfunc", "contents": "search($1)"}, 139 | {"trigger": "search_n\tfunc", "contents": "search_n($1)"}, 140 | {"trigger": "set_difference\tfunc", "contents": "set_difference($1)"}, 141 | {"trigger": "set_intersection\tfunc", "contents": "set_intersection($1)"}, 142 | {"trigger": "set_symmetric_difference\tfunc", "contents": "set_symmetric_difference($1)"}, 143 | {"trigger": "set_union\tfunc", "contents": "set_union($1)"}, 144 | {"trigger": "shrink_to_fit\tfunc", "contents": "shrink_to_fit($1)"}, 145 | {"trigger": "shuffle\tfunc", "contents": "shuffle($1)"}, 146 | {"trigger": "size\tfunc", "contents": "size($1)"}, 147 | {"trigger": "sort\tfunc", "contents": "sort($1)"}, 148 | {"trigger": "sort_heap\tfunc", "contents": "sort_heap($1)"}, 149 | {"trigger": "splice\tfunc", "contents": "splice($1)"}, 150 | {"trigger": "splice_after\tfunc", "contents": "splice_after($1)"}, 151 | {"trigger": "to_string\tfunc", "contents": "to_string($1)"}, 152 | {"trigger": "to_ulong\tfunc", "contents": "to_ulong($1)"}, 153 | {"trigger": "stable_partition\tfunc", "contents": "stable_partition($1)"}, 154 | {"trigger": "stable_sort\tfunc", "contents": "stable_sort($1)"}, 155 | {"trigger": "swap\tfunc", "contents": "swap($1)"}, 156 | {"trigger": "swap_ranges\tfunc", "contents": "swap_ranges($1)"}, 157 | {"trigger": "top\tfunc", "contents": "top($1)"}, 158 | {"trigger": "transform\tfunc", "contents": "transform($1)"}, 159 | {"trigger": "unique\tfunc", "contents": "unique($1)"}, 160 | {"trigger": "unique_copy\tfunc", "contents": "unique_copy($1)"}, 161 | {"trigger": "upper_bound\tfunc", "contents": "upper_bound($1)"}, 162 | {"trigger": "value_comp\tfunc", "contents": "value_comp($1)"}, 163 | ] 164 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/multithreading/atomic.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.c, source.cpp, source.c++", 3 | "completions": 4 | [ 5 | {"trigger": "atomic\tatomic", "contents": "atomic"}, 6 | {"trigger": "atomic_flag\tatomic", "contents": "atomic_flag"}, 7 | {"trigger": "memory_order\tatomic", "contents": "memory_order"}, 8 | {"trigger": "atomic_signal_fence\tatomic", "contents": "atomic_signal_fence($1)"}, 9 | {"trigger": "atomic_thread_fence\tatomic", "contents": "atomic_thread_fence($1)"}, 10 | {"trigger": "kill_dependency\tatomic", "contents": "kill_dependency($1)"}, 11 | {"trigger": "ATOMIC_FLAG_INIT\tatomic", "contents": "ATOMIC_FLAG_INIT($1)"}, 12 | {"trigger": "ATOMIC_VAR_INIT\tatomic", "contents": "ATOMIC_VAR_INIT($1)"}, 13 | {"trigger": "atomic_compare_exchange_strong\tatomic", "contents": "atomic_compare_exchange_strong($1)"}, 14 | {"trigger": "atomic_compare_exchange_strong_explicit\tatomic", "contents": "atomic_compare_exchange_strong_explicit($1)"}, 15 | {"trigger": "atomic_compare_exchange_weak\tatomic", "contents": "atomic_compare_exchange_weak($1)"}, 16 | {"trigger": "atomic_compare_exchange_weak_explicit\tatomic", "contents": "atomic_compare_exchange_weak_explicit($1)"}, 17 | {"trigger": "atomic_exchange\tatomic", "contents": "atomic_exchange($1)"}, 18 | {"trigger": "atomic_exchange_explicit\tatomic", "contents": "atomic_exchange_explicit($1)"}, 19 | {"trigger": "atomic_fetch_add\tatomic", "contents": "atomic_fetch_add($1)"}, 20 | {"trigger": "atomic_fetch_add_explicit\tatomic", "contents": "atomic_fetch_add_explicit($1)"}, 21 | {"trigger": "atomic_fetch_and\tatomic", "contents": "atomic_fetch_and($1)"}, 22 | {"trigger": "atomic_fetch_and_explicit\tatomic", "contents": "atomic_fetch_and_explicit($1)"}, 23 | {"trigger": "atomic_fetch_or\tatomic", "contents": "atomic_fetch_or($1)"}, 24 | {"trigger": "atomic_fetch_or_explicit\tatomic", "contents": "atomic_fetch_or_explicit($1)"}, 25 | {"trigger": "atomic_fetch_sub\tatomic", "contents": "atomic_fetch_sub($1)"}, 26 | {"trigger": "atomic_fetch_sub_explicit\tatomic", "contents": "atomic_fetch_sub_explicit($1)"}, 27 | {"trigger": "atomic_fetch_xor\tatomic", "contents": "atomic_fetch_xor($1)"}, 28 | {"trigger": "atomic_fetch_xor_explicit\tatomic", "contents": "atomic_fetch_xor_explicit($1)"}, 29 | {"trigger": "atomic_flag_clear\tatomic", "contents": "atomic_flag_clear($1)"}, 30 | {"trigger": "atomic_flag_clear_explicit\tatomic", "contents": "atomic_flag_clear_explicit($1)"}, 31 | {"trigger": "atomic_flag_test_and_set\tatomic", "contents": "atomic_flag_test_and_set($1)"}, 32 | {"trigger": "atomic_flag_test_and_set_explicit\tatomic", "contents": "atomic_flag_test_and_set_explicit($1)"}, 33 | {"trigger": "atomic_init\tatomic", "contents": "atomic_init($1)"}, 34 | {"trigger": "atomic_is_lock_free\tatomic", "contents": "atomic_is_lock_free($1)"}, 35 | {"trigger": "atomic_load\tatomic", "contents": "atomic_load($1)"}, 36 | {"trigger": "atomic_load_explicit\tatomic", "contents": "atomic_load_explicit($1)"}, 37 | {"trigger": "atomic_store\tatomic", "contents": "atomic_store($1)"}, 38 | {"trigger": "atomic_store_explicit\tatomic", "contents": "atomic_store_explicit($1)"}, 39 | ] 40 | } -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/clang_greater_version.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Clang Version >= Check 3 | ${1:3}) || (__clang_major__ == ${1:3}) && (__clang_minor__ >= ${2:2})) 5 | ]]> 6 | clanggreater 7 | source.c, source.objc, source.c++, source.objc++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/clang_less.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Clang Version <= Check 3 | 6 | clangless 7 | source.c, source.objc, source.c++, source.objc++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/gcc_greater.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | GCC Version >= Check 3 | ${1:4}) || ((__GNUC__ == ${1:4}) && (__GNUC_MINOR__ >= ${2:7}))) 5 | ]]> 6 | gccgreater 7 | source.c, source.objc, source.c++, source.objc++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/gcc_less.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | GCC Version <= Check 3 | 6 | gccless 7 | source.c, source.objc, source.c++, source.objc++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/if_elif_else_preprocessor.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | #if … #elif … #else 3 | 12 | ifelifpre 13 | source.c, source.objc, source.c++, source.objc++ 14 | 15 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/if_else_preprocessor.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | #if … #else 3 | 10 | ifelsepre 11 | source.c, source.objc, source.c++, source.objc++ 12 | 13 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/preprocessor/ifndef.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | ifndef (…) 3 | 11 | ifnd 12 | source.c, source.objc, source.c++, source.objc++ 13 | 14 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/declval.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | std::declval<T>() 3 | () 5 | ]]> 6 | declv 7 | source.c++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/forward.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | std::forward<T>(…) 3 | (${2:t}) 5 | ]]> 6 | forw 7 | source.c++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/mitlicense.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | MIT License 3 | 28 | mitl 29 | 30 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/namespace.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Namespace 3 | 8 | name 9 | source.c++, source.objc++ 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/template.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Template 3 | 5 | ]]> 6 | tem 7 | source.c++, source.objc++ 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/try-catch.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Try Catch Block 3 | 11 | try 12 | source.c++, source.objc++, source.c++11 13 | 14 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/using_templated.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Using Statement (Templated) 3 | 5 | using ${2:Type} = ${3:typename T::type}; 6 | ]]> 7 | usingt 8 | source.c++ 9 | 10 | -------------------------------------------------------------------------------- /Sublime Text/Packages/C++AutoComplete/utilities/variadic_template.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | Variadic Template 3 | 5 | ]]> 6 | temv 7 | source.c++, source.objc++, source.c++11 8 | 9 | -------------------------------------------------------------------------------- /Sublime Text/Packages/Custom/compare.py: -------------------------------------------------------------------------------- 1 | import sublime_plugin, os, difflib 2 | 3 | ''' 4 | - o1.txt 5 | + o2.txt 6 | -------------------- 7 | +/- diff lines 8 | ++/-- extra lines 9 | ------------------- 10 | 11 | diffrence = " Line\tDiff\n" + diffrence 12 | ''' 13 | 14 | class CompareCommand(sublime_plugin.TextCommand): 15 | def run(self, edit): 16 | 17 | file1 = "D:/dhiraj/Programming/o1.txt" 18 | file2 = "D:/dhiraj/Programming/o2.txt" 19 | 20 | with open(file1,"r") as O1: 21 | o1_lines = O1.readlines() 22 | 23 | with open(file2, "r") as O2: 24 | o2_lines = O2.readlines() 25 | 26 | for i in range(len(o1_lines)): 27 | o1_lines[i] = o1_lines[i].strip() 28 | 29 | for i in range(len(o2_lines)): 30 | o2_lines[i] = o2_lines[i].strip() 31 | 32 | # remove extra empty lines from end 33 | l1 = len(o1_lines) - 1 34 | while(l1 >= 0 and o1_lines[l1] == ""): 35 | o1_lines.pop(l1) 36 | l1 -= 1 37 | 38 | l2 = len(o2_lines) - 1 39 | while(l2 >= 0 and o2_lines[l2] == ""): 40 | o2_lines.pop(l2) 41 | l2 -= 1 42 | 43 | line_no = 0 44 | diffrence = "" 45 | l1 = len(o1_lines) 46 | l2 = len(o2_lines) 47 | 48 | while line_no < l1 or line_no < l2: 49 | #Read the next line from the file 50 | f1_line = f2_line = '' 51 | if(line_no < l1): 52 | f1_line = o1_lines[line_no] 53 | if(line_no < l2): 54 | f2_line = o2_lines[line_no] 55 | 56 | #Increment line counter 57 | line_no += 1 58 | 59 | # Compare the lines from both file 60 | if f1_line != f2_line: 61 | # If a line does not exist on file2 then mark the output with + sign 62 | if f2_line == '' and f1_line != '': 63 | diffrence += " -- {}\t{}\n".format(line_no, f1_line) 64 | # otherwise output the line on file1 and mark it with > sign 65 | elif f1_line != '': 66 | diffrence += " - {}\t{}\n".format(line_no, f1_line) 67 | 68 | # If a line does not exist on file1 then mark the output with + sign 69 | if f1_line == '' and f2_line != '': 70 | diffrence += " ++ {}\t{}\n".format(line_no, f2_line) 71 | # otherwise output the line on file2 and mark it with < sign 72 | elif f2_line != '': 73 | diffrence += " + {}\t{}\n".format(line_no, f2_line) 74 | 75 | if(diffrence == ""): 76 | diffrence = "Both files are identical." 77 | 78 | diff_file = "D:/dhiraj/Programming/diff.txt" 79 | with open(diff_file, "w") as file: 80 | file.write(diffrence) 81 | 82 | execute = 'more "{}" && pause'.format(diff_file) 83 | os.system(execute) 84 | 85 | # echo_diffrence = diffrence.replace('\n', " && echo.") 86 | # os.system("echo {} && pause".format(echo_diffrence)) -------------------------------------------------------------------------------- /Sublime Text/Packages/Custom/file_explorer.py: -------------------------------------------------------------------------------- 1 | import sublime_plugin, os 2 | 3 | class file_explorerCommand(sublime_plugin.TextCommand): 4 | def run(self, edit): 5 | try: 6 | dire = os.path.dirname(self.view.file_name()) 7 | execute = dire[0] + ": && cd " + dire + " && start ." 8 | os.system(execute) 9 | except: 10 | os.system("start d:/dhiraj/Programming") -------------------------------------------------------------------------------- /Sublime Text/Packages/Custom/quote.py: -------------------------------------------------------------------------------- 1 | import sublime_plugin, os 2 | import random 3 | from datetime import datetime 4 | 5 | quotes = [ 6 | [ 7 | "Power is not given to you. You have to take it.", 8 | "-- Beyonce" 9 | ], 10 | [ 11 | "Believe you can and you’re halfway there.", 12 | "-- Theodore Roosevelt" 13 | ], 14 | [ 15 | "Life is either a daring adventure, or nothing.", 16 | "-- Helen Keller" 17 | ], 18 | [ 19 | "By failing to prepare, you are preparing to fail.", 20 | "-- Benjamin Franklin" 21 | ], 22 | [ 23 | "You have power over your mind – not outside events. Realize this, and you will find strength." 24 | "-- Marcus Aurelius" 25 | ], 26 | [ 27 | "I fear not the man who has practiced 10,000 kicks once,", 28 | "but I fear the man who has practiced one kick 10,000 times.", 29 | "-- Bruce Lee" 30 | ], 31 | [ 32 | "The weak can never forgive. Forgiveness is the attribute of the strong.", 33 | "-- Mahatma Gandhi" 34 | ], 35 | [ 36 | "It always seems impossible until it’s done.", 37 | "-- Nelson Mandela" 38 | ], 39 | [ 40 | "Well done is better than well said.", 41 | "-- Benjamin Franklin" 42 | ], 43 | [ 44 | "The future belongs to those who prepare for it today.", 45 | "-- Malcolm X" 46 | ], 47 | [ 48 | "The most courageous act is still to think for yourself.", 49 | "-- Aloud. Coco" 50 | ], 51 | [ 52 | "Wisely, and slow. They stumble that run fast.", 53 | "-- William Shakespeare" 54 | ], 55 | [ 56 | "The successful warrior is the average man, with laser-like focus.", 57 | "-- Bruce Lee" 58 | ], 59 | [ 60 | "I don't measure a man's success by how high he climbs ,", 61 | "but how high he bounces when he hits bottom. ", 62 | "-- George S. Patton" 63 | 64 | ], 65 | [ 66 | "Always remember that you are absolutely unique. Just like everyone else.", 67 | "-- Margaret Mead" 68 | ], 69 | [ 70 | "Remember that the happiest people are not those getting more", 71 | "but those giving more.", 72 | "-- H. Jackson Brown" 73 | ], 74 | [ 75 | "When you consciously choose to be ordinary,", 76 | "You become extraordinary.", 77 | "-- Sadhguru" 78 | ], 79 | [ 80 | "Be happy for this moment. This moment is your life.", 81 | "-- Omar Khayyam" 82 | ], 83 | [ 84 | "Stress is not a natural part of life. Stress comes from your inability", 85 | "to manage your own system.", 86 | "-- Sadhguru" 87 | 88 | ], 89 | [ 90 | "Be the change that you wish to see in the world.", 91 | "-- Mahatma Gandhi" 92 | ], 93 | [ 94 | "Don't walk in front of me.. I may not follow", 95 | "Don't walk behind me.. I may not lead", 96 | "Walk beside me.. just be my friend", 97 | "-- Albert Camus" 98 | ], 99 | [ 100 | "All that we are is the result of what we have thought.", 101 | "-- Buddha" 102 | ], 103 | [ 104 | "Learn from yesterday, live for today, hope for tomorrow.", 105 | "The important thing is not to stop questioning.", 106 | "-- Albert Einstein" 107 | ], 108 | [ 109 | "The fear is simply because you are not living with life,", 110 | "You are living in your mind.", 111 | "-- Sadhguru" 112 | ], 113 | [ 114 | "The purpose of our lives is to be happy.", 115 | "-- Dalai Lama" 116 | ], 117 | [ 118 | "Whatever you are, be a good one.", 119 | "-- Abraham Lincoln" 120 | ], 121 | [ 122 | "When you talk, you are repeating what you already know.", 123 | "But if you listen, you may learn something new.", 124 | "-- Dalai Lama" 125 | ], 126 | [ 127 | "A fish with his mouth closed never gets caught.", 128 | "-- Tony Accardo" 129 | ], 130 | [ 131 | "Try not to become a man of success but rather to become a man of value.", 132 | "-- Albert Einstein" 133 | ], 134 | [ 135 | "Love the life you live. Live the life you love.", 136 | "-- Bob Marley" 137 | ], 138 | [ 139 | "I have not failed. I've just found 10,000 ways that won't work.", 140 | "-- Thomas Edison" 141 | ], 142 | [ 143 | "If you can't explain it simply, you don't understand it well enough.", 144 | "-- Albert Einstein" 145 | ], 146 | [ 147 | "You can't please everyone, and you can't make everyone like you.", 148 | "-- Katie Couric" 149 | ], 150 | [ 151 | "Peace begins with a smile.", 152 | "-- Mother Teresa" 153 | ], 154 | [ 155 | "Success is most often achieved by those who don't know that failure is inevitable.", 156 | "-- Coco Chanel" 157 | ], 158 | [ 159 | "It does not matter how slowly you go, so long as you do not stop.", 160 | "-- Confucius" 161 | ], 162 | [ 163 | "Don't cry because it's over, smile because it happened.", 164 | "-- Dr. Seuss" 165 | ], 166 | [ 167 | "Be yourself. Everyone else is already taken.", 168 | "-- Oscar Wilde" 169 | ], 170 | [ 171 | "You only live once, but if you do it right, once is enough.", 172 | "-- Mae West" 173 | ], 174 | [ 175 | "Your limitation it's only your imagination.", 176 | ], 177 | [ 178 | "The harder you work for something, the greater you'll feel when you achieve it.", 179 | ], 180 | [ 181 | "Don't stop when you're tired. Stop when you're done.", 182 | ], 183 | [ 184 | "Any fool can write code that a computer can understand.", 185 | "Good programmers write code that humans can understand.", 186 | "-- Martin Fowler" 187 | ], 188 | [ 189 | "The happiest moment i felt is that moment when i realized my ability to create.", 190 | "-- Dr. Hazem Ali" 191 | ], 192 | [ 193 | "Wake up with determination. Go to bed with satisfaction.", 194 | ], 195 | [ 196 | "Experience is the name everyone gives to their mistakes.", 197 | "-- Oscar Wilde" 198 | ], 199 | [ 200 | "Knowledge is power.", 201 | "-- Francis Bacon" 202 | ], 203 | [ 204 | "Do one thing every day that scares you.", 205 | "-- Eleanor Roosevelt" 206 | ], 207 | [ 208 | "Keep your face to the sunshine and you can never see the shadow.", 209 | "-- Helen Keller" 210 | ], 211 | [ 212 | "In order to be irreplaceable, one must always be different.", 213 | "-- Coco Chanel" 214 | ], 215 | [ 216 | "Simplicity is the soul of efficiency.", 217 | "-- Austin Freeman" 218 | ], 219 | [ 220 | "Act as if what you do makes a difference. It does.", 221 | "-- William James" 222 | ], 223 | [ 224 | "Life is like riding a bicycle. To keep your balance, you must keep moving.", 225 | "-- Albert Einstein" 226 | ], 227 | [ 228 | "Limit your \"always\" and your \"nevers\".", 229 | "-- Amy Poehler​​" 230 | ], 231 | [ 232 | "Make it work, make it right, make it fast.", 233 | "-- Kent Beck" 234 | ], 235 | [ 236 | "Inspiration comes from within yourself. One has to be positive.", 237 | "When you're positive, good things happen.", 238 | "-- Deep Roy" 239 | ], 240 | [ 241 | "You must do the things you think you cannot do.", 242 | "-- Eleanor Roosevelt" 243 | ], 244 | [ 245 | "Without great solitude, no serious work is possible.", 246 | "-- Pablo Picasso" 247 | ] 248 | ] 249 | 250 | class QuoteCommand(sublime_plugin.TextCommand): 251 | def run(self, edit): 252 | 253 | now = datetime.now() 254 | cur_date = now.strftime("%d %B %Y ~ %A") 255 | cur_time = now.strftime("%I:%M:%S %p") 256 | 257 | # without quote 258 | info = '''/* 259 | ** author : Dhiraj Govindvira 260 | ** date : {} 261 | ** time : {} 262 | */ 263 | \n'''.format(cur_date, cur_time) 264 | 265 | # with quote 266 | # ind = random.randrange(len(quotes)) 267 | # info = '/*' 268 | # for q in quotes[ind]: 269 | # info += "\n** " + q 270 | # info += ''' 271 | # ** 272 | # ** Author : Dhiraj Govindvira 273 | # ** Date : {} 274 | # ** Time : {} 275 | # */\n'''.format(cur_date, cur_time) 276 | 277 | file_path = self.view.file_name() 278 | if(os.path.exists(file_path)): 279 | with open(file_path, "r") as file: 280 | myCode = file.readlines() 281 | 282 | # update 283 | if len(myCode) >= 6: 284 | if myCode[1] == "** author : Dhiraj Govindvira\n": 285 | if myCode[5] == "\n": 286 | myCode = myCode[6::] 287 | else: 288 | myCode = myCode[5::] 289 | 290 | code = "" 291 | for line in myCode: 292 | code += line 293 | 294 | with open(file_path, "w", encoding = 'utf-8') as file: 295 | file.write(info + code) 296 | -------------------------------------------------------------------------------- /Sublime Text/Packages/Custom/terminal.py: -------------------------------------------------------------------------------- 1 | import sublime_plugin, os 2 | 3 | class terminalCommand(sublime_plugin.TextCommand): 4 | def run(self, edit): 5 | try: 6 | dire = os.path.dirname(self.view.file_name()) 7 | execute = dire[0] + ": && cd " + dire + " && start" 8 | os.system(execute) 9 | except: 10 | os.system("pushd d:/dhiraj/Programming && start") -------------------------------------------------------------------------------- /Sublime Text/Packages/README.md: -------------------------------------------------------------------------------- 1 | ## Custom packages sublime text 2 | 3 | - **C++ Auto Complete** : auto complete for c++ 4 | - **Terminal** : open command prompt for current directory 5 | - **File Explorer** : open file explorer for current directory 6 | - **Compare** : comapre o1.txt and o2.txt 7 | - **Quote** : add quote and user info in current file 8 | 9 | ### Use : 10 | - copy all folder into `C:\Users\...\AppData\Roaming\Sublime Text 3\Packages` 11 | - `Preferences => key bindings` add keyboard shortcut 12 | ```json 13 | [ 14 | { "keys": ["f5"], "command": "quote" }, 15 | { "keys": ["f6"], "command": "compare"}, 16 | { "keys": ["ctrl + `"], "command": "terminal" }, 17 | { "keys": ["ctrl+shift+`"], "command": "file_explorer" }, 18 | ] 19 | ``` -------------------------------------------------------------------------------- /Sublime Text/README.md: -------------------------------------------------------------------------------- 1 | ## My Sublime Text setup for coding (C++ and Windows) 2 | 3 | ``` 4 | same steps can be followed for other languages 5 | - Install sublime 6 | - Install compiler for language 7 | - Add build file for language 8 | - ctrl + b to Run :) 9 | ``` 10 | 11 | ### 1. MinGW Compiler 12 | * Install Mingw from https://sourceforge.net/projects/mingw/ or https://osdn.net/projects/mingw/releases/ 13 | * Youtube video : https://youtu.be/sXW2VLrQ3Bs 14 | * Your path should preferably be `C:\MinGW`. Finally, add the bin directory, `C:\MinGW\bin` to the System PATH. 15 | (you can also change that path but make sure to add in System PATH, so that we can access `g++` from anywhere) 16 | 17 | ### 2. Sublime Text 18 | * Install https://www.sublimetext.com/ 19 | 20 | ### 3. Create a build system 21 | * Open `Sublime > Go to Tools > Build System > New Build System`. 22 | * Paste [this build](https://github.com/Dhiraj-01/CP/blob/main/Sublime%20Text/Build/C%2B%2B.sublime-build) in the file and save as `C++.sublime-build`. 23 | * Select custom build `Tools > Build System > C++.sublime-build`. 24 | 25 | ### 4. Let's check 26 | * write some code. 27 | * select build for C++ language (`Sublime > Go to Tools > Build System > New Build System > C++.sublime-build`). 28 | * `ctrl + b` to run your code (that logic is writtern in build file) 29 | * `ctrl + shift + b` to navigate between build options 30 | 31 | ### 5. Infinite loop solution 32 | * open terminal and paste `taskkill /IM a.exe /F` 33 | * `ctrl + e` in sublime, for that `Preferences => key bindings` add this keyboard shortcut 34 | `{ "keys": ["ctrl + e"], "command": "cancel_build" },` 35 | * Task Manager `a.exe => end task` 36 | 37 | 38 | [OdatNurd - Sublime Text Tutorials](https://www.youtube.com/channel/UCJAB_XF3kAMqwF85y0hxcXQ) 39 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/all.sublime-completions: -------------------------------------------------------------------------------- 1 | { 2 | "scope": "source.cpp, source.c++", 3 | "completions": 4 | [ 5 | "DHIRAJ", 6 | "endl", 7 | "break", 8 | "return", 9 | "first", 10 | "second", 11 | "true", 12 | "false", 13 | "continue", 14 | "bitset<32>", 15 | "string", 16 | "vector", 17 | "length()", 18 | "size()", 19 | "decltype($1)", 20 | "isupper($1)", 21 | "islower($1)", 22 | "toupper($1)", 23 | "tolower($1)", 24 | "isdigit($1)", 25 | "isalpha($1)", 26 | "isalnum($1)", 27 | "assert($1)", 28 | "__builtin_popcountll($1)", 29 | "to_ullong()", 30 | "long long int", 31 | "long double", 32 | "unsigned long long int", 33 | "LLONG_MAX", 34 | "LLONG_MIN", 35 | "INT_MAX", 36 | "INT_MIN", 37 | "ULLONG_MAX", 38 | 39 | { 40 | "trigger": "get\tget(tuple)", 41 | "contents": "get<${1:index}>(${2:tuple})", 42 | }, 43 | { 44 | "trigger": "probinfo", 45 | "contents": "/*\nTitle : \nProblem : \nTime Comp. : \nSpace Comp. : \n*/" 46 | }, 47 | { 48 | "trigger": "dash", 49 | "contents": "cerr << string(80,'-') << '\\n';" 50 | }, 51 | { 52 | "trigger": "memset", 53 | "contents": "memset($1, $2, sizeof($1));" 54 | }, 55 | { 56 | "trigger": "using\tnamespace", 57 | "contents": "using namespace ${1:name};\n" 58 | }, 59 | { 60 | "trigger": "all()\tbegin-end", 61 | "contents": "${1:v}.begin(), ${1:v}.end()" 62 | }, 63 | { 64 | "trigger": "rall()\trbegin-rend", 65 | "contents": "${1:v}.rbegin(), ${1:v}.rend()" 66 | }, 67 | 68 | { "trigger": "mp\tmake_pair", "contents": "make_pair($1)" }, 69 | { "trigger": "mt\tmake_tuple", "contents": "make_tuple($1)" }, 70 | { "trigger": "pb\tpush_back", "contents": "push_back($1)" }, 71 | { "trigger": "eb\templace_back", "contents": "emplace_back($1)" }, 72 | 73 | { "trigger": "exist\ts.find()", "contents": "(${1:s}.find(${2:val}) != ${1:s}.end())" }, 74 | { "trigger": "arr\tscan(arr)", "contents": "ll n;\ncin >> n;\n\nvector a(n);\ncin >> a;" }, 75 | { "trigger": "print\tprint(arr)", "contents": "for(auto &X : $1) {\n\tcout << X << \" \";\n}\ncout << endl;" }, 76 | { "trigger": "ppair\tprint(pair)", "contents": "for(auto &X : $1) {\n\tcout << X.first << \" \" << X.second << endl;\n}" }, 77 | { "trigger": "precision", "contents": "cout << fixed << setprecision($1);" }, 78 | { "trigger": "nl", "contents": "cout << \"\\n\";" }, 79 | { "trigger": "pi", "contents": "const double PI = acos(-1); // 3.14159265358979323846" }, 80 | { "trigger": "gcd\t__gcd(a,b)", "contents": "__gcd($1)" }, 81 | { "trigger": "lcm\t(a * b) / gcd(a, b)", "contents": "(($1) / (__gcd($1, $2)) * ($2))" }, 82 | { "trigger": "Yes", "contents": "cout << \"Yes\\n\";" }, 83 | { "trigger": "No", "contents": "cout << \"No\\n\";" }, 84 | { "trigger": "YES", "contents": "cout << \"YES\\n\";" }, 85 | { "trigger": "NO", "contents": "cout << \"NO\\n\";" }, 86 | { "trigger": "put", "contents": "cout << (($1) ? \"YES\" : \"NO\") << endl;" }, 87 | { "trigger": "flush", "contents": "cout.flush();" }, 88 | 89 | { "trigger": "pq\tpriority_queue", "contents": "priority_queue<${1:ll}>" }, 90 | { "trigger": "pq\tpriority_queue(comp)", "contents": "priority_queue<${1:ll}, vector<${1:ll}>, decltype(comp)> pq(comp)" }, 91 | { "trigger": "minpq\tpriority_queue", "contents": "priority_queue<${1:ll}, vector<${1:ll}>, greater<${1:ll}>>" }, 92 | { "trigger": "list", "contents": "list<${1:ll}>" }, 93 | { "trigger": "que\tqueue", "contents": "queue<${1:ll}>" }, 94 | { "trigger": "deq\tdeque", "contents": "deque<${1:ll}>" }, 95 | { "trigger": "stack\tstack", "contents": "stack<${1:ll}>" }, 96 | { "trigger": "tuple", "contents": "tuple<${1:ll}>" }, 97 | { "trigger": "multiset", "contents": "multiset<${1:ll}>" }, 98 | { "trigger": "unorderset\tunordered_set", "contents": "unordered_set<${1:ll}>" }, 99 | { "trigger": "multimap", "contents": "multimap<${1:ll}, ${2:ll}>" }, 100 | { "trigger": "unordermap\tunordered_map", "contents": "unordered_map<${1:ll}, ${2:ll}>" }, 101 | 102 | // "pair<${1:ll},${2:ll}>", 103 | // "queue<${1:ll}>", 104 | // "set<${1:ll}>", 105 | // "list<${1:ll}>", 106 | // "deque<${1:ll}>", 107 | // "stack<${1:ll}>", 108 | // "multiset<${1:ll}>", 109 | // "unordered_set<${1:ll}>", 110 | // "multimap<${1:ll},${2:ll}>", 111 | // "unordered_map<${1:ll},${2:ll}>", 112 | // "priority_queue<${1:ll}>", 113 | ] 114 | } -------------------------------------------------------------------------------- /Sublime Text/Snippets/bit-manipulation.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | bitmanipulate 17 | 18 | source.c++ 19 | 20 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/boostlib.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 4 | using namespace boost::multiprecision; 5 | ]]> 6 | 7 | boost 8 | 9 | source.c++ 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/c++ function.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 19 | 20 | cpp 21 | 22 | source.c++ 23 | 24 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/c++.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 4 | using namespace std; 5 | 6 | #ifdef DHIRAJ 7 | #include "D:/dhiraj/Programming/debug.h" 8 | #else 9 | #define dbg(...) 1 10 | #define cerr if(0) cerr 11 | #endif 12 | 13 | using ll = long long int; 14 | #define endl '\n' 15 | 16 | template 17 | inline istream& operator >> (istream& in, pair& p) { 18 | in >> p.first >> p.second; 19 | return in; 20 | } 21 | template 22 | inline istream& operator >> (istream& in, vector& v) { 23 | for(T& x : v) in >> x; 24 | return in; 25 | } 26 | 27 | void solve(ll &tc) 28 | { 29 | ${0:} 30 | } 31 | 32 | int main() 33 | { 34 | #ifdef DHIRAJ 35 | freopen("D:/dhiraj/Programming/i1.txt", "r", stdin); 36 | freopen("D:/dhiraj/Programming/o1.txt", "w", stdout); 37 | freopen("D:/dhiraj/Programming/e1.txt", "w", stderr); 38 | #endif 39 | 40 | ios::sync_with_stdio(0); 41 | cin.tie(0); 42 | 43 | ll rep = ${3:1}; 44 | while(rep--) { 45 | ll tc = 1;${2: 46 | cin >> tc;} 47 | for(ll i = 1; i <= tc; i++) { 48 | // cout << "Case #" << i << ": "; 49 | cerr << "Case #" << i << "\n"; 50 | solve(i); 51 | } 52 | if(dbg()) break; 53 | } 54 | return 0; 55 | } 56 | ]]> 57 | 58 | cpp 59 | 60 | source.c++ 61 | 62 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/debug.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | debug 13 | 14 | source.c++ 15 | 16 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/for.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | for loop 3 | 6 | 7 | for 8 | 9 | source.c++ 10 | 11 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/function.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | ${1:f} = []($2) 4 | { 5 | $0 6 | }; 7 | ]]> 8 | 9 | function 10 | 11 | source.c++ 12 | 13 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/graph.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | > g; 7 | 8 | void clear_all(ll nn) 9 | { 10 | nn += 1; 11 | g.assign(nn, vector()); 12 | \}} 13 | cin >> n >> m; 14 | clear_all(n); 15 | 16 | for(ll i = 0; i < m; i++) 17 | { 18 | ll u, v; 19 | cin >> u >> v; 20 | g[u].emplace_back(v); 21 | g[v].emplace_back(u); 22 | } 23 | ]]> 24 | 25 | graph 26 | 27 | source.c++ 28 | 29 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/grid.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | dx = {1, -1, 0, 0, 1, -1, 1, -1}; 4 | const vector dy = {0, 0, 1, -1, 1, -1, -1, 1}; 5 | const vector direction = {"D", "U", "R", "L", "RD", "LU", "LD", "RU"}; 6 | 7 | bool ok(ll x, ll y, ll nn, ll mm, bool index = 0) { 8 | return (x >= 0 + index and x < nn + index and y >= 0 + index and y < mm + index); 9 | } 10 | 11 | ]]> 12 | 13 | grid 14 | 15 | source.c++ 16 | 17 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/hashing.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | > 30)) * 0xbf58476d1ce4e5b9; 9 | x = (x ^ (x >> 27)) * 0x94d049bb133111eb; 10 | return x ^ (x >> 31); 11 | } 12 | 13 | size_t operator()(uint64_t x) const 14 | { 15 | static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); 16 | return fxn(x + FIXED_RANDOM); 17 | } 18 | }; 19 | ]]> 20 | 21 | hash 22 | 23 | source.c++ 24 | 25 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/in-range.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 4 | bool in(const T& x, const T& lo, const T& hi) { 5 | return lo <= x and x <= hi; 6 | } 7 | ]]> 8 | 9 | inrange 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/input-output.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | file input output 3 | 11 | 12 | inout 13 | 14 | source.c++, source.c 15 | 16 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/lambda-comparator.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | lambda comparator 3 | 8 | 9 | comp 10 | 11 | source.c++ 12 | 13 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/modulo.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | modulo 12 | 13 | source.c++ 14 | 15 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/n-dimensional vector.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | N dimensional vector 3 | 5 | struct vec : public vector> { 6 | static_assert(D >= 1, "Vector dimension must be greater than zero!"); 7 | template 8 | vec(int n = 0, Args... args) : vector>(n, vec(args...)) { 9 | } 10 | }; 11 | template 12 | struct vec<1, T> : public vector { 13 | vec(int n = 0, const T& val = T()) : vector(n, val) { 14 | } 15 | }; 16 | ]]> 17 | 18 | nvector 19 | 20 | source.c++ 21 | 22 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/random-number.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | r) swap(l, r); 7 | return std::uniform_int_distribution(l, r)(rng); 8 | // return std::uniform_real_distribution(l, r)(rng); 9 | } 10 | 11 | ]]> 12 | 13 | random 14 | 15 | source.c++ 16 | 17 | -------------------------------------------------------------------------------- /Sublime Text/Snippets/to_string.sublime-snippet: -------------------------------------------------------------------------------- 1 | 2 | debug 3 | 15 | 16 | to_string 17 | 18 | source.c++ 19 | 20 | --------------------------------------------------------------------------------