{"id":22831275,"url":"https://github.com/masum184e/problem_solving_techniques","last_synced_at":"2025-03-31T01:48:12.434Z","repository":{"id":195789738,"uuid":"693665831","full_name":"masum184e/problem_solving_techniques","owner":"masum184e","description":"Unmasking the array of daily techniques utilized in competitive programming. Throughout your competitive programming journey, these techniques will assist you in making your solutions a little bit more efficient.","archived":false,"fork":false,"pushed_at":"2025-03-02T21:02:24.000Z","size":51,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-02T22:18:46.432Z","etag":null,"topics":["array","big-integer","bit-masking","competitve-programming","data-structures-and-algorithms","dsa","number-theory","programming-contest","recursion"],"latest_commit_sha":null,"homepage":"https://codeforces.com/profile/masum1834e","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/masum184e.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-19T13:25:20.000Z","updated_at":"2025-03-02T21:02:28.000Z","dependencies_parsed_at":"2025-02-06T07:10:52.707Z","dependency_job_id":"7cd0aeb9-4400-47c7-829c-ff053228afb5","html_url":"https://github.com/masum184e/problem_solving_techniques","commit_stats":null,"previous_names":["masum184e/problem_solving_techniques"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masum184e%2Fproblem_solving_techniques","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masum184e%2Fproblem_solving_techniques/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masum184e%2Fproblem_solving_techniques/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masum184e%2Fproblem_solving_techniques/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/masum184e","download_url":"https://codeload.github.com/masum184e/problem_solving_techniques/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246403895,"owners_count":20771526,"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","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":["array","big-integer","bit-masking","competitve-programming","data-structures-and-algorithms","dsa","number-theory","programming-contest","recursion"],"created_at":"2024-12-12T20:19:56.684Z","updated_at":"2025-03-31T01:48:12.420Z","avatar_url":"https://github.com/masum184e.png","language":"C++","readme":"# Topic Discussed\n\n- Array\n  - Generate Subbarray\n  - Unique Pair\n- Big Integers\n  - Addition\n- Bit Masking\n  - Arithmetic\n  - Bit Operation\n  - Even Odd\n  - Power\n  - Swap\n- Graph\n  - Combinatorics\n    - Binomial Coeefficient\n    - Birthday Paradox\n    - Catalan Number\n    - Number of digit in factorial\n    - Zero at end of factorial\n  - Prime\n    - Is Prime\n    - Prime Factorization\n    - Segmented Sieve\n    - Sieve of Eratosthenes\n  - Chinese Remainder\n  - Divisors\n  - Euler Totient\n  - Exponentiation\n  - Euclidean\n  - Extended Euclidean\n  - Matrix Exponentiation\n  - Modulo Arithmetic\n  - Fast Multiplication\n  - MMI\n- Number Theory\n  - Shortestpath BFS\n- Searching\n  - Lower-Upper Bound\n- Sorting\n  - Inversion Count\n  - Is Sorted\n\n# Contents\n\n- [Recursion](#recursion)\n- Overflow Underflow\n- Bit Indexing\n- Even Odd\n- Time Limit\n- Sum\n- Reverse VS Sort\n- Binary Search\n\n# Recursion\n\n## Direction\n\n### Forward Recursion\n\nThe recursive calls happen first, and any operations are performed after all recursive calls have returned.\n\n```cpp\nvoid forwardRecursion(int n) {\n    if (n == 0) {\n        return;\n    }\n    forwardRecursion(n - 1);\n    cout \u003c\u003c n \u003c\u003c \" \";\n}\n// 1 2 3 4 5 6 7 8 9 10\n```\n\n### Backward Recursion\n\nThe operations are performed before the recursive calls. Each recursive call processes its action and then makes the next recursive call.\n\n```cpp\nvoid backwardRecursion(int n) {\n    if (n == 0) {\n        return;\n    }\n    cout \u003c\u003c n \u003c\u003c \" \";\n    backwardRecursion(n - 1);\n}\n// 10 9 8 7 6 5 4 3 2 1\n```\n\n## How Recursion Uses the Stack\n\nIn recursion, each function call is pushed onto the stack. When a function finishes, it's popped off the stack, and execution returns to the previous function call.\n\n**1. Function Call:** Each recursive call adds a new frame to the stack, containing:\n\n    - Local variables\n    - Parameters\n    - Return address (where to resume after the function ends)\n\n**2. Base Case:** When the base case is hit, the recursion stops — the function returns, and the stack starts unwinding.\n\n**3. Stack Unwinding:** As each function returns, its frame is popped off the stack, and the result is passed back to the previous frame.\n\n### Understaing Call Stack Behavior\n\nFor sum of N integer, suppose you input `num = 3`. The recursive function works like this:\n\n1. `sum(3)` is called → Push `sum(3)` onto the stack\n2. `sum(2)` is called → Push `sum(2)` onto the stack\n3. `sum(1)` is called → Push `sum(1)` onto the stack\n4. `sum(0)` is called → Push `sum(0)` onto the stack\n   At this point, the stack looks like this:\n\n```sql\n| sum(0) |\n| sum(1) |\n| sum(2) |\n| sum(3) |   \u003c-- Top of the stack\n```\n\n**🛑 Base Case Reached:**\n\n- `sum(0)` returns 0, and the stack starts unwinding.\n\n**🔓 Stack Unwinding:**\n\n- `sum(1)` pops from the stack and returns `1`.\n- `sum(2)` pops from the stack and returns `2 + 1 = 3`.\n- `sum(3)` pops from the stack and returns `3 + 3 = 6`.\n\n### Tree Recursion\n\nA function makes more than one recursive call to itself. This creates a recursive tree-like structure where each function call branches into multiple sub-calls.\n\n**Optimize Tree Recursion:**\n\n- Memoization\n- Tabulation\n- Iteration\n\n### Tracing Tree\n\n**Forward Recursion:**\n\n```js\n                      forwardPrint(3)\n                            |\n                    forwardPrint(2)\n                            |\n                    forwardPrint(1)\n                            |\n                    forwardPrint(0)  \u003c- Base case\n                            |\n                          Print(1)\n                            |\n                          Print(2)\n                            |\n                          Print(3)\n```\n\n**Backward Recursion:**\n\n```s\n                      backwardPrint(3)\n                            |\n                         Print(3)\n                            |\n                    backwardPrint(2)\n                            |\n                         Print(2)\n                            |\n                    backwardPrint(1)\n                            |\n                         Print(1)\n                            |\n                    backwardPrint(0) \u003c- Base case\n```\n\n# Overflow and Underflow\n\nOverflow occurs when a variable is assigned a value greater than its maximum limit, causing the value to **wrap around** and produce incorrect results.\n\n- An `int` is typically `32` bits, so it can store values between `-2^31` and `2^31 - 1` (i.e., `-2147483648` to `2147483647`).\n\nIf you assign a value larger than `2147483647` to an `int`, overflow will occur. For example:\n\n```cpp\nint x = INT_MAX;\ncout \u003c\u003c \"Before overflow: \" \u003c\u003c x \u003c\u003c endl;\nx = x + 1;\ncout \u003c\u003c \"After overflow: \" \u003c\u003c x \u003c\u003c endl;\n```\n\nHere, adding `1` to `2147483647` causes the variable to wrap around to the minimum value `-2147483648`.\n\n**Underflow** does the same but in the opposite direction.\n\n## How to avoid overflow and underflow\n\n1. **Use larger data types:** consider data types with larger ranges, such as `long long`\n2. **Check for overflow/underflow conditions manually:**\n\n```cpp\nif(x \u003e INT_MAX){\n    // Handle overflow case\n}\n```\n\n3. **Modular arithmatic:** you can use modulo `10^9+7` as a limit to avoid overflow.\n\n# Bit Manipulation\n\n## Indexing\n\n### 0-Based Indexing\n\nThe bit at position 0 represents the least significant bit (LSB), and the bit at position n−1 represents the most significant bit (MSB).\n\n```cpp\nint number = 5;\nbool isBitSet = (number \u0026 (1 \u003c\u003c 0)) != 0;\n```\n\n### 1-Based Indexing\n\nThe bit at position 1 represents the least significant bit, and the bit at position n represents the most significant bit.\n\n```cpp\nint number = 5;\nbool isBitSet = (number \u0026 (1 \u003c\u003c (1 - 1))) != 0;\n```\n\n# Even and Odd\n\n- count the number of even number between range [a, b]:\n  $$\n  \\left\\lfloor \\frac{b}{2} \\right\\rfloor - \\left\\lfloor \\frac{a-1}{2} \\right\\rfloor\n  $$\n  count the number of odd number between range [a, b]:\n  $$\n  (b - a + 1) - \\left( \\left\\lfloor \\frac{b}{2} \\right\\rfloor - \\left\\lfloor \\frac{a-1}{2} \\right\\rfloor \\right)\n  $$\n- $$ {even}^{even}={even} $$\n- $$ {odd}^{odd}={odd} $$\n- $$ {even}+{even}={even} $$\n- $$ {odd}+{odd}={even} $$\n- $$ {even}+{odd}={odd} $$\n\n# Time Limit\n\n| **Input Size (n)** | **Allowed Time Complexity**                         | **Operation Limit (\\(\\approx 10^8\\) per second)**       |\n| ------------------ | --------------------------------------------------- | ------------------------------------------------------- |\n| $ \\ n\\leq10^6 \\ $  | $ \\ O(n) \\ $, $ \\ O(nlog n) \\ $                     | Up to **10 million** iterations (linear and log-linear) |\n| $ \\ n\\leq10^5 \\ $  | $ \\ O(n) \\ $, $ \\ O(nlog n) \\ $, $ \\ O({n}^{2}) \\ $ | Up to **10 billion** iterations (quadratic)             |\n| $ \\ n\\leq10^4 \\ $  | $ \\ O(\\text{n}^\\text{2}) \\ $, $ \\ O(n\\sqrt{n}) \\ $  | **Quadratic and sub-quadratic operations**              |\n| $ \\ n\\leq10^3 \\ $  | $ \\ O(\\text{n}^{3}) \\ $                             | **Cubic operations** allowed                            |\n| $ \\ n\\leq100 \\ $   | $ \\ O({2}^{n}) \\ $, $ \\ O(n!) \\ $                   | **Exponential operations** allowed                      |\n\n# Sum\n\n- sum of integers in the range [a,b]:\n  $$ \\frac{b-a+1}{2} \\times (a + b) $$\n\n# Reverse vs Sort\n\n## reverse\n\n- time complexity is O(n)\n- doesn't consider the original order\n\n## sort\n\n- time complexity is O(nlogn)\n- changes order based on sorting criteria\n\n# Binary Search\n\n1. Sort the array\n2. Define search space\n3. Handle corner cases\n4. Conditional Check\n5. Function building\n6. Update search space\n7. Extract solution\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasum184e%2Fproblem_solving_techniques","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasum184e%2Fproblem_solving_techniques","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasum184e%2Fproblem_solving_techniques/lists"}