{"id":21422006,"url":"https://github.com/omarelgabry/cubes","last_synced_at":"2025-10-05T09:20:58.477Z","repository":{"id":87066855,"uuid":"42825660","full_name":"OmarElgabry/Cubes","owner":"OmarElgabry","description":"Implementation of interesting algorithms in C++","archived":false,"fork":false,"pushed_at":"2021-07-27T19:06:43.000Z","size":176,"stargazers_count":24,"open_issues_count":0,"forks_count":6,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T04:13:12.071Z","etag":null,"topics":["algorithm-challenges","algorithms","c-plus-plus","competitive-programming"],"latest_commit_sha":null,"homepage":null,"language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OmarElgabry.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","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":"2015-09-20T19:10:33.000Z","updated_at":"2023-10-14T22:23:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"f52c321f-f88a-4793-bf88-59a9d79500d6","html_url":"https://github.com/OmarElgabry/Cubes","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/OmarElgabry/Cubes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmarElgabry%2FCubes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmarElgabry%2FCubes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmarElgabry%2FCubes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmarElgabry%2FCubes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OmarElgabry","download_url":"https://codeload.github.com/OmarElgabry/Cubes/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OmarElgabry%2FCubes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265262588,"owners_count":23736422,"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":["algorithm-challenges","algorithms","c-plus-plus","competitive-programming"],"created_at":"2024-11-22T20:41:22.665Z","updated_at":"2025-10-05T09:20:53.429Z","avatar_url":"https://github.com/OmarElgabry.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Screenshot](https://raw.githubusercontent.com/OmarElGabry/Cubes/master/assets/logo.png)\n\n# Cubes\n\nImplementation of interesting algorithms in C++ and their related problems on online Judges.\n\n## Index\n+ [Numbers](#numbers) \n+ [Bits](#bits)\n+ [Strings](#strings)\n+ [Sorting](#sorting)\n+ [Cumulative Array](#cumulative-array) \n+ [Window](#window)\n+ [Two Pointers](#two-pointers)\n+ [Recursion](#recursion)\n+ [BFS](#bfs)  \n+ [DFS](#dfs)\n+ [DP](#dp)\n+ [Randoms](#randoms)\n+ [Support](#support)\n+ [Contribute](#contribute)\n+ [License](#license)\n\n\n## Numbers\u003ca name=\"numbers\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Is Prime      | Check if a number is prime or not. | ```Input:```\u003cbr\u003e 5 \u003cbr\u003e ```Output:``` \u003cbr\u003e  true |O(sqrt(n))| — |\n| Prime Numbers | Returns all prime numbers till **N** using Sieve implementation. | ```Input:``` \u003cbr\u003e 10 \u003cbr\u003e ```Output:``` \u003cbr\u003e  {2, 3, 5, 7} | O(n * ln * ln)| [B. Prime Matrix](http://codeforces.com/problemset/problem/271/B) |\n| Divisors | Returns all divisors of a number. | ```Input:``` \u003cbr\u003e 10 \u003cbr\u003e ```Output:``` \u003cbr\u003e  {1, 10, 2, 5}  | O(sqrt(n)) | [B. Easy Number Challenge](http://codeforces.com/problemset/problem/236/B), [B. Duff in Love](http://codeforces.com/problemset/problem/588/B) |\n| Factorize | Returns all prime factors of a number. | ```Input:``` \u003cbr\u003e 4 \u003cbr\u003e ```Output:``` \u003cbr\u003e  {2, 2} |O(sqrt(n)) | — |\n| Count Range Divisors | Returns the count of divisors for each number from 1 to **N**. | ```Input:``` \u003cbr\u003e 10 \u003cbr\u003e ```Output:``` \u003cbr\u003e  27 | O(N) | — |\n| Factorial | Returns the factorial of a number. |  ```Input:``` \u003cbr\u003e 5 \u003cbr\u003e ```Output:``` \u003cbr\u003e  120 | O(N) | — |\n| GCD \u0026 LCM | Returns the greatest common divisor and least common multiple of given two numbers. | ```Input:``` \u003cbr\u003e 6, 28 \u003cbr\u003e ```Output:``` \u003cbr\u003e   GCD=2, LCM=84 | — | [A. Fox and Number Game](http://codeforces.com/problemset/problem/389/A)\n| Big Mod | Given **B**, **P**, \u0026 **M**, Find: B^P % M.| ```Input:```\u003cbr\u003e B=2147483647, P=2147483647, M=46340\u003cbr\u003e ```Output:``` \u003cbr\u003e  13903 | O(LogP) | [Big Mod](https://uva.onlinejudge.org/index.php?option=onlinejudge\u0026page=show_problem\u0026problem=310)\n\n## Bits\u003ca name=\"bits\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Bit Manipulations | Implementing various snippets: \u003cbr\u003e• Check if bit 0 or 1. \u003cbr\u003e • Set a bit to 1. \u003cbr\u003e• Given N, return 2^k, k is th position of least significant 1-bit.\u003cbr\u003e • Given n1 \u0026 n2, get number of different bits.\u003cbr\u003e• Count number of 1 bits. |   _**Check** Test Cases in src/Bits/Bit Manipulations_ | O(N) | [B. Fedor and New Game](http://codeforces.com/problemset/problem/467/B), \u003cbr\u003e[B. The Child and Set](http://codeforces.com/problemset/problem/437/B)\n| Binary Conversions | Convert decimal to binary number(integer/string), and back to decimal.  | ```Input:``` \u003cbr\u003e10 \u003cbr\u003e ```Output:``` \u003cbr\u003e  {\"1010\", 10} | O(logN), O(N) | [B. New Year and Old Property](http://codeforces.com/problemset/problem/611/B)\n| Generate Combinations | Generate combinations with all possible sizes. | ```Input:``` \u003cbr\u003e{1, 2} \u003cbr\u003e ```Output:``` \u003cbr\u003e  {}, {1}, {2}, {1, 2} | O(N * 2^N) | [B. Preparing Olympiad](http://codeforces.com/problemset/problem/550/B), \u003cbr\u003e[324. Problem Set](http://a2oj.com/p.jsp?ID=324)\n\n## Strings\u003ca name=\"strings\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Remove Consecutive | Remove consecutive characters. | ```Input:``` \u003cbr\u003e \"abacabaabacabaa\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  a |O(N)| [A. Plug-in](http://codeforces.com/problemset/problem/81/A), [Parentheses Balance](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=614)\n| Alpha Characters | Get all strings with only consecutive aplha characters | ```Input:``` \".omar.is.brilliant.\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  {omar, is, brilliant} | O(N) | [Andy's First Dictionary](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=1756)\n| Palindrome Characters | Check if a string is Palindrome.  | ```Input:``` \u003cbr\u003e abcba \u003cbr\u003e ```Output:``` \u003cbr\u003e  true | O(N) | [Pesky Palindromes](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=289), [C. Little Frog](http://codeforces.com/problemset/problem/53/C), [Mother bear](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=1886)\n| Palindrome Substrings | Check if a string is Palindrome by substrings of length **K**; Comparing substrings instead of characters. \u003cbr\u003e_Given that the length of string is divisible by K._ | ```Input:``` \"abckfgabc\", K=3 \u003cbr\u003e ```Output:``` \u003cbr\u003e  true | O(N) | —\n| Sub Strings | Generates all possible substrings.  | ```Input:``` \u003cbr\u003e \"hello\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  {h, he, hel, hell, hello, e, ...} | O(N^3) | [Pesky Palindromes](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=289)\n| Words Values | Return summation of values for each word(if exist) in the given string.  | ```Input:``` \u003cbr\u003e string=\"hello world\", values={\"hello\" =\u003e 5, \"john\" =\u003e 2}\u003cbr\u003e ```Output:``` \u003cbr\u003e  5 | O(N) | [Hay Points](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=1236), [Babelfish](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=1223)\n| Frequent Character | Count frequency of each character in a string, and get the max character(s) occurred.  | ```Input:``` \u003cbr\u003e \"The characTer T is The mosT frequenT characTer in This sTring\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  character=T, count=9 | O(N) | —\n| Sequence of Characters | Check if a sequence of characters exists or not in the given string. | ```Input:``` \u003cbr\u003e string=\"can you find the given characters in order?\", sequence=\"fire\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  true | O(N) | [B. Suffix Structures](http://codeforces.com/problemset/problem/448/B)\n| Anagram | Given string **A** and **B**, return true if A occurs as an anagram in B.| ```Input:``` \u003cbr\u003e A=\"car\", B=\"xdfacrcytvharc\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  true \u003cbr\u003e_at (3,5), (11,13)_| O(A*B) | —\n| Replace Characters | Given a string of lower case characters, and a set of queries, each query has two characters. \u003cbr\u003e For every query, Replace first character with second in the given string, and vice-versa, and then return the resulting string. | ```Input:``` \u003cbr\u003estring=\"aabbccdd\", queries={('a', 'b'), ('c', 'd'), ('d', 'a')} \u003cbr\u003e ```Output:``` \u003cbr\u003e  bbddaacc| O(max(N, queries)) | [B. Rebranding](http://codeforces.com/problemset/problem/591/B)\n| Hamming Distance Sum | Given a string **X** of length \u003c= 10^5 with 1s and 0s, and string **Y** with length \u003c= length of X. Get summation of numbers of different bits for each sub-string of X with string Y.| ```Input:``` \u003cbr\u003eY=\"01\", X=\"00111\"\u003cbr\u003e ```Output:``` \u003cbr\u003e  3 | O(N) | [B. Hamming Distance Sum](http://codeforces.com/contest/608/problem/B)\n\n## Sorting\u003ca name=\"sorting\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Count Sort | Sort an array by counting.  | ```Input:```\u003cbr\u003e {3, 1, 0, 1, 4, 8, 11, 4, 34, 2} \u003cbr\u003e ```Output:``` \u003cbr\u003e  {0, 1, 1, 2, 3, 4, 4, 8, 11, 34} | O(NLogN) | [A. Helpful Math](http://codeforces.com/problemset/problem/339/A)\n| Reverse Subarray | Is it possible to sort the array by reversing exactly one segment(part) of it?  | ```Input:``` \u003cbr\u003e {6, 78, 63, 59, 28, 24, 8, 96, 99, 120} \u003cbr\u003e ```Output:``` \u003cbr\u003e  [1 -\u003e 6] | O(N) | [B. Sort the Array](http://codeforces.com/problemset/problem/451/B)\n| Shifting | Find minimum number of operations to sort array by moving the last element to the beginning  | ```Input:``` \u003cbr\u003e{4, 5, 6, 2, 3} \u003cbr\u003e ```Output:``` \u003cbr\u003e  2 | O(N) | [B. Little Pony and Sort by Shift](http://codeforces.com/problemset/problem/454/B)\n| Stairs | What's the minimum number of array elements to be erased \u003cbr\u003e so that we can have array in this form: \u003cbr\u003e_a1 \u003c a2 \u003c ... \u003c ai - 1 \u003c ((ai)) \u003e ai + 1 \u003e ... \u003e an - 1 \u003e an._   | ```Input:``` \u003cbr\u003e {1, 1, 2, 2, 3, 3} \u003cbr\u003e ```Output:``` \u003cbr\u003e  min=1, array={1, 2, 3, 2, 1} | O(N) | [B. Sereja and Stairs](http://codeforces.com/problemset/problem/381/B)\n\n\n## Cumulative Array\u003ca name=\"cumulative-array\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Range Sum(1D Array) | Given an array, and set of queries: _start_ and _end_, what is sum of values in range [start, end]. | ```Input:``` \u003cbr\u003earray={1, 3, 4, 2, 5}, start=2, end=5\u003cbr\u003e ```Output:``` \u003cbr\u003e  14 | O(N) | —\n| Max 1D Subarray(Fixed Length) | Given array, what's the max sub array of length **L**. | ```Input:```\u003cbr\u003earray={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, L=3\u003cbr\u003e ```Output:``` \u003cbr\u003e  27 | O(N) | —\n| Adjacent Characters | Given 2D array **N** x **M** of '.' \u0026 '#' characters, and set of queries, each query with two numbers c1 \u0026 c2. For each query, return the summation of numbers of two adjacent '.' characters in each row from column c1 to c2. | ```Input:``` \u003cbr\u003e array=\u003cbr\u003e `....#..#` \u003cbr\u003e `.#......` \u003cbr\u003e `##.#....` \u003cbr\u003e `##..#.##` \u003cbr\u003e `........` \u003cbr\u003eN=5,M=8,\u003cbr\u003equeries={(1,3),(2,5)}\u003cbr\u003e\u003cbr\u003e ```Output:``` \u003cbr\u003e  {6, 9} | O(N * M), O(N * queries) | [C. New Year and Domino](http://codeforces.com/contest/611/problem/C)\n\n## Window\u003ca name=\"window\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Max 1D Subarray(Fixed Length) | Given array, what's the max sub array of length **L**. | ```Input:```\u003cbr\u003e array={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, L=3\u003cbr\u003e ```Output:``` \u003cbr\u003e  27 | O(N) | —\n| Max 1D Subarray(Variable Length) | Given array, find subarray with maximum sum. | ```Input:``` \u003cbr\u003e{1, 10, -3, 2, -40, -4, 5, 3, 18, -2}\u003cbr\u003e ```Output:``` \u003cbr\u003e  range=[6, 8], sum=26 | O(N) | [Maximum Sum](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=44)\n| Max 2 Subarrays | Given array, find the largest 2 sub arrays(not interleaved) of length **L**. | ```Input:``` \u003cbr\u003earray={1, 2, 1, 15, 2, 3, 6, 8, 3, 3, 8, 6}, L=3\u003cbr\u003e ```Output:``` \u003cbr\u003e  [3 -\u003e 5], [6 -\u003e 8] | O(N) | [B. Maximum Absurdity](http://codeforces.com/problemset/problem/332/B)\n\n\n## Two Pointers\u003ca name=\"two-pointers\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Max Subarray(Less than or equal value) | Given array, find max subarray \u003c= **t**. | ```Input:```\u003cbr\u003e array={6, 8, 14, 9, 4, 11, 10}, t=13\u003cbr\u003e ```Output:``` \u003cbr\u003e  [3 -\u003e 4]| O(N) | [B. Books](http://codeforces.com/problemset/problem/279/B)\n| Max Subarray of Similar Characters | Given a string contains only two characters; 'a' \u0026 'b'. _You can change at most K characters; either 'a' to 'b' or vice-versa_. Find max subarray. | ```Input:```\u003cbr\u003e str=\"aabaabaa\", K=1 \u003cbr\u003e ```Output:``` \u003cbr\u003e  aabaa | O(N) | [C. Vasya and String](http://codeforces.com/contest/676/problem/C)\n| Guess the Number | Given a set of queries like: \"\u003e=1\", \"\u003c2\", \"\u003e-3\", ...etc.\u003cbr\u003eGuess the range of numbers that achieves these queries. | ```Input:``` \u003cbr\u003equeries={{\"\u003e=\", 1}, {\"\u003e=\", 3}, {\"\u003e\", -3}, {\"\u003c=\", 55}}\u003cbr\u003e ```Output:``` \u003cbr\u003e  [3, 55]| O(N) | [416A - Guess a number!](http://codeforces.com/problemset/problem/416/A)\n| Zuma | Given array, and a _tnum_ number that will be inserted at index _tindex_. \u003cbr\u003eIf there are three or more contiguous similar numbers, they should be destroyed(erased). \u003cbr\u003eThis rule is applied until there are no more three or more contiguous similar numbers. \u003cbr\u003e Count the destroyed numbers. \u003cbr\u003e _Initially, there is no three or more contiguous similar numbers._ | ```Input:``` \u003cbr\u003e array={5, 4, 4, 2, 2, 4, 4, 5, 5, 1, 7, 6}, tnum=2, tindex=4\u003cbr\u003e ```Output:``` \u003cbr\u003e  10| O(N) | [B. Balls Game](http://codeforces.com/problemset/problem/430/B)\n| Exact Sum | Given array of 10^5 elements, Find All **Xs** \u0026 **Ys**, where X + Y = goal | ```Input:``` {1, 2, 4, 6, 10, 5, 13, 8, 14, 5}, goal=10\u003cbr\u003e ```Output:``` \u003cbr\u003e  {2, 8}, {4, 6}, {5, 5}| O(NLogN) | [Exact Sum](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=1998)\n| 3SUM | Given array of 10^5 elements, Find All **Is** \u0026 **Js** \u0026 **Ks**, where I + J + K = goal. | ```Input:``` \u003cbr\u003earray={1, 2, 4, 6, 10, 5, 13, 8, 14, 5}, goal=10\u003cbr\u003e ```Output:``` \u003cbr\u003e  {1, 4, 5}| O(N^2) | —\n\n## Recursion\u003ca name=\"recursion\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Print Number | Given a number print it and print number in bits recursively. | ```Input:``` \u003cbr\u003e213 \u003cbr\u003e ```Output:``` \u003cbr\u003e  decimal=213, binary=11010101  | O(N) | —\n| Fibonacci | Calculate the fibonanci value of N, and print the first N numbers in the fibonanci series. | ```Input:``` \u003cbr\u003e7 \u003cbr\u003e ```Output:``` \u003cbr\u003e  F(n)=13, series={0,1,1,2,3,5,8} | — | —\n| 3n+1 | Given a number, if even divide by 2, if odd, multiply by 3, and add 1. \u003cbr\u003eCount the steps till number = 1 recursively. | ```Input:``` \u003cbr\u003e7 \u003cbr\u003e ```Output:``` \u003cbr\u003e  17 | — | [The 3n + 1 problem](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=36)\n\n## BFS\u003ca name=\"bfs\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| 4\u00267 | Generate all possible children nodes \u003cbr\u003e starting from 4 \u0026 7 until you find the goal. | ```Input:``` \u003cbr\u003egoal=447 \u003cbr\u003e ```Output:``` \u003cbr\u003e  {4, 7, 44, 47, 74, 77, 447} | O(2^len(goal)) | [B. Lucky Numbers (easy)](http://codeforces.com/problemset/problem/96/B)\n\n## DFS\u003ca name=\"dfs\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Permutation | Given array, generate all permutations. | ```Input:```\u003cbr\u003e {4, 5, 6}\u003cbr\u003e ```Output:``` \u003cbr\u003e  {{4, 5, 6}, {4, 6, 5}, {5, 4, 6}, ...} | O(N * N!) _Printing the numbers takes O(N)._ | [Generating Fast](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=1039)\n| Combination | Given array, generate all combinations of length **L**. | ```Input:``` \u003cbr\u003e array={1, 7, 2}, L=2\u003cbr\u003e ```Output:``` \u003cbr\u003e  {1, 2}, {1, 7}, {7, 2} | O(N! / ( (N-L)! * L!)) | —\n| Beautiful Numbers | Generate all possible numbers that has a number of digits **N** and consists of any number from 1 to **K**. | ```Input:```\u003cbr\u003e K=2, N=3\u003cbr\u003e ```Output:``` \u003cbr\u003e  {{1, 1, 1}, {1, 1, 2}, {1, 2, 1}, ...} | O(K^N) | [BNUMBERS](http://www.spoj.com/problems/BNUMBERS/)\n| Max Path | Given 2D array, starting from (0, 0), move only to right and down. \u003cbr\u003eFind the maximum sum by exploring all possible paths. Can you improve your solution (hint: using DP (Recursive and [Iteration](https://leetcode.com/problems/minimum-path-sum/discuss/364593/Java-O(N*M)-time-0(1)-space-solution-(in-place))) / Dijkstra (MinPath) )? | ```Input:``` \u003cbr\u003e {{1, 5}, {2, 4}}\u003cbr\u003e ```Output:``` \u003cbr\u003e  10 | — | [Minimum Path Sum](https://leetcode.com/problems/minimum-path-sum/)\n| Maze | Given 2D array, starting from (0, 0), move to up, left, right and down. \u003cbr\u003eFind the goal node by exploring all possible paths. | ```Input:``` \u003cbr\u003e array=\u003cbr\u003e `. . X .` \u003cbr\u003e `. X . G` \u003cbr\u003e `. . . X` \u003cbr\u003e `X . X X` \u003cbr\u003e\u003cbr\u003e ```Output:``` \u003cbr\u003e  [1, 3] | — | [C. Maze](http://codeforces.com/problemset/problem/378/C)\n| Connected Cells | Given 2D array, starting from (0, 0), Return the number of connected blocks| ```Input:``` \u003cbr\u003e array=\u003cbr\u003e `. . X .` \u003cbr\u003e `X X X X` \u003cbr\u003e `. . . X` \u003cbr\u003e `X . X X` \u003cbr\u003e \u003cbr\u003e ```Output:``` \u003cbr\u003e  3 | — | —\n| Generate Binaries | Generate all binary numbers of length **N** and has given number of 1s L, \u003cbr\u003e. Print them sorted (in ascending lexicographical order) | ```Input:``` \u003cbr\u003e N=4, L=2\u003cbr\u003e ```Output:``` \u003cbr\u003e  {0011, 0101, 0110, 1001, 1010, 1100}| O(N! / ( (N-L)! * L!)) | [The Hamming Distance Problem](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=670)\n| Lowest Common Ancestor | Given two nodes in a tree(with and without parent pointers).\u003cbr\u003e Return their lowest common ancestor.|  _**Check** Test Case in src/DFS/Lowest Common Ancestor_ \u003cbr\u003e **Check** [Amazon Live Coding - Number of edges between two nodes](https://gist.github.com/OmarElGabry/cad9c61949ebb4a1691d066bc3173012) | O(h), O(N) | —\n\n## DP\u003ca name=\"dp\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \t|\n| Fibonacci | Calculate the [fibonanci](https://en.wikipedia.org/wiki/Fibonacci_number) value of N. | ```Input:``` \u003cbr\u003e 5 \u003cbr\u003e ```Output:``` \u003cbr\u003e  8 | O(2N ~ N) | —\n| Divide | Given a number, get f(n) where f(n) = f(n/2) + f(n/2), f(0) = f(1) = 1, and n \u003c= 2^31. \u003cbr\u003e_(i.e. If n = 7, then f(7) = f(3) + f(4))_| ```Input:``` 7 \u003cbr\u003e ```Output:``` \u003cbr\u003e  7 | — | [TRUCKL](http://www.spoj.com/problems/TRUCKL/)\n\n## Randoms\u003ca name=\"randoms\"\u003e\u003c/a\u003e\n| Code Name\t\t| Problem Statement | Test Case | Complexity | Related Problems |\n| ------------- | ------------- | ------------- | -----      | ----- \n| Unique Numbers | Given array of numbers, return count of unique values, and print them.| ```Input:``` \u003cbr\u003e{7, 2, 4, 5, 16, 2, 0, 2, 4, 6}\u003cbr\u003e ```Output:``` \u003cbr\u003e  count=7, numbers={7, 2, 4, 5, 16, 0, 6} | O(N) | [A. Inna and Alarm Clock](http://codeforces.com/problemset/problem/390/A), [A. Valera and Antique Items](http://codeforces.com/problemset/problem/441/A), [A. I Wanna Be the Guy](http://codeforces.com/problemset/problem/469/A), [A. Asphalting Roads](http://codeforces.com/problemset/problem/583/A), [A. Bulbs](http://codeforces.com/contest/615/problem/A)\n| Unique Characters | Given string of characters, what's the count of unique characters and print them.  | ```Input:``` \u003cbr\u003e \"hMshMZ\" \u003cbr\u003e ```Output:``` \u003cbr\u003e  count=4,\u003cbr\u003e characters={h, M, s, Z} | O(N) | [A. Inna and Alarm Clock](http://codeforces.com/problemset/problem/390/A), [A. Valera and Antique Items](http://codeforces.com/problemset/problem/441/A), [A. I Wanna Be the Guy](http://codeforces.com/problemset/problem/469/A), [A. Asphalting Roads](http://codeforces.com/problemset/problem/583/A), [A. Bulbs](http://codeforces.com/contest/615/problem/A)\n| Equal Lists | What's the min number of strings to be removed so that both lists can have same strings regardless of their order. | ```Input:``` \u003cbr\u003e list1={ \"foo\", \"bar\", \"baz\", \"foo\", \"foo\", \"yard\" }, \u003cbr\u003elist2={ \"bar\", \"bar\", \"baz\", \"yard\", \"foo\", \"yard\" } \u003cbr\u003e ```Output:``` \u003cbr\u003e  4 | O(N) | [Just Prune The List](https://uva.onlinejudge.org/index.php?option=com_onlinejudge\u0026Itemid=8\u0026page=show_problem\u0026problem=3200)\n| Unique Sets(Fixed Length) | Generate max number of unique sets of length **L**. \u003cbr\u003eIt's not valid to use one array element more than once.| ```Input:```\u003cbr\u003e {1, 2, 3, 4, 4, 4, 7, 8, 9, 10}, L=2\u003cbr\u003e ```Output:``` \u003cbr\u003e  {{4, 10}, {4, 9}, {8, 7}, {4, 3}, {2, 1}}| O(NlongN) | —\n| Unique Sets(largest possible sizes) | Generate \u0026 Get the max number of unique sets with largest possible sizes. | ```Input:``` \u003cbr\u003e {1, 2, 3, 4, 1, 2, 3, 1, 2, 1}\u003cbr\u003e ```Output:``` \u003cbr\u003e  {{1,2,3,4}, {1,2,3}, {1,2}, {1}}, max=4 | O(N) | [B. Beautiful Paintings](http://codeforces.com/problemset/problem/651/B)\n| The Block Number | Given an array of numbers. Print the number of ways you can choose contiguous subarrays of length **L**. Given that any subarray shouldn't contain number \u003e= _block_ number | ```Input:``` \u003cbr\u003e array={2, 2, 0, 7, 3, 2, 9, 2, 3, 1} block=7, L=3\u003cbr\u003e ```Output:``` 2; _at (0, 2)_ and 1 _at (7, 9)_ \u003cbr\u003e   | O(N) | [B. Prison Transfer](http://codeforces.com/problemset/problem/427/B), [A. Queue on Bus Stop](http://codeforces.com/problemset/problem/435/A), [C. DZY Loves Sequences](http://codeforces.com/contest/447/problem/C), [B. Domino Effect](http://codeforces.com/problemset/problem/405/B), [A. Alena's Schedule](http://codeforces.com/problemset/problem/586/A), [A. PawnChess](http://codeforces.com/problemset/problem/592/A), [B. Chocolate](http://codeforces.com/problemset/problem/617/B), [C. Watchmen](http://codeforces.com/problemset/problem/651/C), [B. Books](http://codeforces.com/problemset/problem/279/B)\n| Generate Subarrays | Given array, Loop through each subarray of length **L**. | ```Input:``` {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, L=5\u003cbr\u003e ```Output:``` \u003cbr\u003e  {12345, 23456, 34567, ...} | O(N*L) | —\n| Painting Grid | Given a grid, and set of operations, each operation will paint all cells in a row or a column in a color(initial color is 0). \u003cbr\u003eWhat's the resulting grid.\u003cbr\u003e_Each operation consists of 3 numbers; row or column(row=1 or col=0), index (1-indexed), and color._ | ```Input:``` \u003cbr\u003e{{1, 1, 3}, {0, 2, 1}, {1, 2, 2}}\u003cbr\u003e ```Output:``` \u003cbr\u003e  {{3,1,3}, {2,2,2}, {0,1,0}} | O(NxM) | [B. Print Check](http://codeforces.com/problemset/problem/631/B)\n| Total Attacks | How many tanks can attack each other? \u003cbr\u003eGiven N tanks, each tank represented by a number. Two tanks can only attack each other if they have the same number. Return number of total attacks. | ```Input:``` \u003cbr\u003e {1, 2, 5, 1, 5, 7, 2, 6, 2, 6}\u003cbr\u003e ```Output:``` \u003cbr\u003e  6 | O(N) | [B. Wet Shark and Bishops](http://codeforces.com/problemset/problem/621/B), \u003cbr\u003e [C. Watchmen](http://codeforces.com/problemset/problem/651/C)\n| The K-th Element | Given array of 10^5 element, which generates the following sequence of numbers. \u003cbr\u003eFor example, array=[10,4,18,3,...], generates the sequence: \"10,10,4,10,4,18,10,4,18,3,...\". \u003cbr\u003eReturn the K-th(1-Indexed) number. | ```Input:``` \u003cbr\u003e array={10, 4, 18, 3}, K=5 \u003cbr\u003e ```Output:``` \u003cbr\u003e  4 | O(N) | [B. Game of Robots](http://codeforces.com/problemset/problem/670/B), \u003cbr\u003e [A. Summer Camp](http://codeforces.com/problemset/problem/672/A)\n| Cakes | Given two arrays with **N** ingredients, and additional **K** grams. \u003cbr\u003eThe 1st array for the needed grams, and 2nd for the available grams for each ingredient. \u003cbr\u003eReturn the max number of cakes we can bake using the additional K grams. \u003cbr\u003e\u003cbr\u003e_• The needed grams for each ingredient can bake one cake, i.e available=4, needed=2, then cakes=4/2=2_\u003cbr\u003e _• To prepare one cookie you need to use all N ingredients._ | ```Input:``` \u003cbr\u003e needed={4, 3, 5, 6}, available={11, 12, 14, 20}, K=3 \u003cbr\u003e ```Output:``` \u003cbr\u003e  3 | O(NxK) | [D1. Magic Powder - 1](http://codeforces.com/problemset/problem/670/D1)\n| Regular Expression | Applying common regular expression patterns for matching, \u003cbr\u003esearching and replacing strings using [regex](http://www.cplusplus.com/reference/regex/). |  **Check** _src/Randoms/Regular Expression_ | — | —\n\n## Support \u003ca name=\"support\"\u003e\u003c/a\u003e\nI've written these snippets in my free time during my studies. If you find it useful, please support the project by spreading the word.\n\n## Contribute \u003ca name=\"contribute\"\u003e\u003c/a\u003e\n\nContribute by creating new issues, sending pull requests on Github or you can send an email at: omar.elgabry.93@gmail.com\n\n## License \u003ca name=\"license\"\u003e\u003c/a\u003e\nBuilt under [MIT](http://www.opensource.org/licenses/mit-license.php) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomarelgabry%2Fcubes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomarelgabry%2Fcubes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomarelgabry%2Fcubes/lists"}