{"id":23108902,"url":"https://github.com/archishmansengupta/competitive-coding","last_synced_at":"2025-07-23T06:05:30.071Z","repository":{"id":47372667,"uuid":"322200697","full_name":"ArchishmanSengupta/competitive-coding","owner":"ArchishmanSengupta","description":"my random competitive programming solutions (and other stuff) ~ 3 years ago, yes ik i need to make a comeback","archived":false,"fork":false,"pushed_at":"2022-11-09T04:20:38.000Z","size":7927,"stargazers_count":12,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T15:50:53.666Z","etag":null,"topics":["competitive-programming","cpp","icpc"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ArchishmanSengupta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-17T06:21:35.000Z","updated_at":"2025-03-02T18:44:58.000Z","dependencies_parsed_at":"2023-01-22T17:46:20.709Z","dependency_job_id":null,"html_url":"https://github.com/ArchishmanSengupta/competitive-coding","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ArchishmanSengupta/competitive-coding","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArchishmanSengupta%2Fcompetitive-coding","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArchishmanSengupta%2Fcompetitive-coding/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArchishmanSengupta%2Fcompetitive-coding/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArchishmanSengupta%2Fcompetitive-coding/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ArchishmanSengupta","download_url":"https://codeload.github.com/ArchishmanSengupta/competitive-coding/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ArchishmanSengupta%2Fcompetitive-coding/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266626115,"owners_count":23958344,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-23T02:00:09.312Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["competitive-programming","cpp","icpc"],"created_at":"2024-12-17T01:31:51.915Z","updated_at":"2025-07-23T06:05:30.048Z","avatar_url":"https://github.com/ArchishmanSengupta.png","language":"C++","readme":"## Introduction  \u003c!-- omit in toc --\u003e\nThis is the list of modern CPP tricks often used in Coding Interviews and Competitive Programming.  \n\n## Contents: \u003c!-- omit in toc --\u003e\n- [No more nested `min(x, min(y, ...))`](#no-more-nested-minx-miny-)\n- [JavaScript like Destructuring using Structured Binding in C++](#javascript-like-destructuring-using-structured-binding-in-c)\n- [Powerful Logging and Debugging](#powerful-logging-and-debugging)\n  - [How debug macros work?](#how-debug-macros-work)\n  - [The Problem with this macro - its not scalable](#the-problem-with-this-macro---its-not-scalable)\n  - [Solution using a powerful macro](#solution-using-a-powerful-macro)\n- [Generic Reader and Writer for multiple variables and containers](#generic-reader-and-writer-for-multiple-variables-and-containers)\n- [Decorators in C++ and Multiple Parameters](#decorators-in-c-and-multiple-parameters)\n  - [Live Demo on YouTube](#live-demo-on-youtube)\n  - [Printing as many variables in one line](#printing-as-many-variables-in-one-line)\n  - [Powerful decorator functions in C++](#powerful-decorator-functions-in-c)\n  - [Exploiting decorators by nesting them](#exploiting-decorators-by-nesting-them)\n\n\n## No more nested `min(x, min(y, ...))`\nUse initializer list and `std::min` and `std::max` to make life easy\n```cpp\nsmall = min(x, min(y, min(z, k))); // the old way\nsmall = min({x, y, z, k}); // life is easy\n```\n\n## JavaScript like Destructuring using Structured Binding in C++\n```cpp\npair\u003cint, int\u003e cur = {1, 2};\nauto [x, y] = cur;\n// x is now 1, y is now 2\n// no need of cur.first and cur.second\n\narray\u003cint, 3\u003e arr = {1, 0, -1};\nauto [a, b, c] = arr;\n// a is now 1, b is now 0, c is now -1\n```\n\n\n----------------\n\n\n## Powerful Logging and Debugging\n\n### How debug macros work?\nStraight to the point, I have often used the `debug` macro which stringifies the variable names and their values.\n\n```cpp\n#define debug(x) cout \u003c\u003c #x \u003c\u003c \" \" \u003c\u003c x \nint ten = 10;\ndebug(ten); // prints \"ten = 10\"\n```\n\nThis is often useful in debugging.\n\n### The Problem with this macro - its not scalable\nHowever, when you have multiple variables to log, you end up with more `deb2` and `deb3` macros.\n\n```cpp\n#define debug(x) cout \u003c\u003c #x \u003c\u003c \" \" \u003c\u003c x \n#define debug2(x) cout \u003c\u003c #x \u003c\u003c \" \" \u003c\u003c x \u003c\u003c \" \"  \u003c\u003c #y \u003c\u003c \" \" \u003c\u003c y \n#define debug3(x, y, z) cout \u003c\u003c #x \u003c\u003c \" \" \u003c\u003c x \u003c\u003c \" \"  \u003c\u003c #y \u003c\u003c \" \" \u003c\u003c y \u003c\u003c \" \"  \u003c\u003c #z \u003c\u003c \" \" \u003c\u003c z \n```\n\nThis is not scalable.\n\n### Solution using a powerful macro\nHere is the solution using variadic macros and fold expressions,\n\n```cpp\n#define debug(...) logger(#__VA_ARGS__, __VA_ARGS__)\ntemplate\u003ctypename ...Args\u003e\nvoid logger(string vars, Args\u0026\u0026... values) {\n    cout \u003c\u003c vars \u003c\u003c \" = \";\n    string delim = \"\";\n    (..., (cout \u003c\u003c delim \u003c\u003c values, delim = \", \"));\n}\n\nint xx = 3, yy = 10, xxyy = 103;\ndebug(xx); // prints \"xx = 3\"\ndebug(xx, yy, xxyy); // prints \"xx, yy, xxyy = 3, 10, 103\"\n```\n\n\n----------------\n\n\n## Generic Reader and Writer for multiple variables and containers\n```cpp\ntemplate \u003ctypename... T\u003e\nvoid read(T \u0026...args) {\n    ((cin \u003e\u003e args), ...);\n}\n\ntemplate \u003ctypename... T\u003e\nvoid write(string delimiter, T \u0026\u0026...args) {\n    ((cout \u003c\u003c args \u003c\u003c delimiter), ...);\n}\n\ntemplate \u003ctypename T\u003e\nvoid readContainer(T \u0026t) {\n    for (auto \u0026e : t) {\n        read(e);\n    }\n}\n\ntemplate \u003ctypename T\u003e\nvoid writeContainer(string delimiter, T \u0026t) {\n    for (const auto \u0026e : t) {\n        write(delimiter, e);\n    }\n    write(\"\\n\");\n}\n```\n### Usage\n```cpp\n// Question: read three space seprated integers and print them in different lines.\n\tint x, y, z;\n\tread(x, y, z);\n\twrite(\"\\n\", x, y, z);\n\t\n// even works with variable data types :)\n\tint n;\n\tstring s;\n\tread(n, s);\n\twrite(\" \", s, \"has length\", n, \"\\n\");\n\t\n// Question: read an array of `N` integers and print it to the output console.\n\tint N;\n\tread(N);\n\tvector\u003cint\u003e arr(N);\n\treadContainer(arr);\n\twriteContainer(\" \", arr); // output: arr[0] arr[1] arr[2] ... arr[N - 1]\n\twriteContainer(\"\\n\", arr);\n\t/**\n\t* output:\n\t* arr[0]\n\t* arr[1]\n\t* arr[2]\n\t* ...\n\t* ...\n\t* ...\n\t* arr[N - 1]\n\t*/\n```\n\n\n----------------\n\n\n## Decorators in C++ and Multiple Parameters\n\n\n### Printing as many variables in one line\n```cpp\ntemplate\u003ctypename ...T\u003e\nvoid printer(T\u0026\u0026... args) {\n    ((cout \u003c\u003c args \u003c\u003c \" \"), ...);\n}\n\nint age = 25;\nstring name = \"Archishman\";\nprinter(\"I am\", name, ',', age, \"years old\"); \n// ^ This prints the following\n// I am Archishman, 18 years old\n```\n\n### Powerful decorator functions in C++\n```cpp\ntemplate\u003ctypename F\u003e\nauto debug_func(const F\u0026 func) {\n    return [func](auto \u0026\u0026...args) { // forward reference\n        cout \u003c\u003c \"input = \";\n        printer(args...);\n        auto res = func(forward\u003cdecltype(args)\u003e(args)...);\n        cout \u003c\u003c \"res = \" \u003c\u003c res \u003c\u003c endl;\n        return res;\n    };\n}\n\ndebug_func(pow)(2, 3);\n// ^ this automatically prints\n// input = 2 3 res = 8\n```\n\n### Exploiting decorators by nesting them\nLets define another decorator `beautify` as follows.\n```cpp\ntemplate\u003ctypename F\u003e\nauto beautify(const F\u0026 func) {\n    return [func](auto \u0026\u0026...args) { // forward reference\n        cout \u003c\u003c \"========\" \u003c\u003c endl;\n        func(forward\u003cdecltype(args)\u003e(args)...);\n        cout \u003c\u003c \"========\" \u003c\u003c endl;\n    };\n}\n\nbeautify(debug_func(pow(2, 3)));\n// ^ this now prints\n// ========\n// input = 2 3 res = 8\n// ========\n```\nIts amazing how much you can do by writing such generic decorators and nest them.  \nThink about decorators like `log_time` that calculates the time taken for a given function.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farchishmansengupta%2Fcompetitive-coding","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farchishmansengupta%2Fcompetitive-coding","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farchishmansengupta%2Fcompetitive-coding/lists"}