{"id":20375650,"url":"https://github.com/juliolmuller/hash-k-complementary-problem","last_synced_at":"2026-06-11T09:31:42.281Z","repository":{"id":110656274,"uuid":"298895136","full_name":"juliolmuller/hash-k-complementary-problem","owner":"juliolmuller","description":"Usage of Hash Table data structure to present an elegant  solution to the k-Complementary Problem (Problema dos Números k-Complementares).","archived":false,"fork":false,"pushed_at":"2023-04-12T03:05:21.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-12-05T01:43:42.569Z","etag":null,"topics":["c","data-structures","hash-table","hashing","problema"],"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/juliolmuller.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":"2020-09-26T20:32:57.000Z","updated_at":"2023-04-12T03:05:24.000Z","dependencies_parsed_at":"2023-04-14T18:31:58.996Z","dependency_job_id":null,"html_url":"https://github.com/juliolmuller/hash-k-complementary-problem","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/juliolmuller/hash-k-complementary-problem","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliolmuller%2Fhash-k-complementary-problem","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliolmuller%2Fhash-k-complementary-problem/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliolmuller%2Fhash-k-complementary-problem/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliolmuller%2Fhash-k-complementary-problem/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juliolmuller","download_url":"https://codeload.github.com/juliolmuller/hash-k-complementary-problem/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juliolmuller%2Fhash-k-complementary-problem/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34192870,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-11T02:00:06.485Z","response_time":57,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","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":["c","data-structures","hash-table","hashing","problema"],"created_at":"2024-11-15T01:32:19.470Z","updated_at":"2026-06-11T09:31:42.260Z","avatar_url":"https://github.com/juliolmuller.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  :hash: The k-Complementary Problem\n\u003c/h1\u003e\n\n- **Project proposed by:** [Lucila Bento, Vinícius de Sá \u0026 Jayme Szwarcfiter](https://www.academia.edu/20715778/Hashing_na_solu%C3%A7%C3%A3o_de_problemas_at%C3%ADpicos)\n- **Project developed by:** [Julio L. Muller](https://github.com/juliolmuller)\n- **Released on:** Out 5, 2020\n- **Updated on:** Out 5, 2020\n- **Latest version:** 1.0.0\n- **License:** MIT\n\nThe main objective of this project is to develop a solution in C language to the **k-complementary problem** or the **Pair Sum Problem** using the *hash table* data structure. This was based on a [brazilian paper](https://www.academia.edu/20715778/Hashing_na_solu%C3%A7%C3%A3o_de_problemas_at%C3%ADpicos) with other 3 problems that can be solved with hash tables.\n\nBeing constant `k` a positive integer number and being `A` an unordered list of `|A| = n` integers, the ***k*-Complementary Problem** consists on determining all `{x, y}` pairs of elements within `A`, since the sum of `x` and `y` is equal to `k`.\n\nGiven an positive integer `k` and a list `A` of `n` unordered integers, we must find out whether there is a pair `{x, y} ⊂ A` such that `x + y = k`. This is a particular case of the well-known *Subset Sum Problem*, in which there are no restrictions on the size of the subsets summing `k`.\n\nIf we use, as an example, the inputs `A = [1, -4, 18, 11, 2, 9, -3, 5, 560, 10]` and `k = 20`, the outputs will be the pairs of numbers that satisfy the condition: `{2, 18}`, `{9, 11}` and `{10, 10}`.\n\nFor a naive solution, we just need to check all pairs `{x, y}` of elements of `A`. If `x + y` equals `k`, return it and stop. Although no extra space is required, the number of additions and comparisons to be performed is *O*(n²) in the worst case.\n\nWe want to do better. First note that, if we select an element `x` of `A`, we can look for its complement `y = k - x` in `A`, and the problem becomes now a search problem. If, for each `x`, we need to traverse the whole list to check whether it contains its complement, then our algorithm still demands *O*(n²) time. A better alternative would be to first sort the list `A`, and then, for each `x ∈ A`, look for its complement using binary search, whereupon our time complexity would be *O*(n log n).\n\nWe can use a hash set to store the elements of `A`. The amount of space we need is that to store `n` integers (scattered along their respective buckets) plus an underlying array with `n / α` positions. For a constant load factor `α`, that means *O*(n) space, while the average time of all dictionary operations is as good as *O*(1) under the simple uniform hashing assumption. The overall expected time of such algorithm is therefore also *O*(n), corresponding to the *O*(n) insertions and lookups in the hash set, in the worst case.\n\n## :trophy: Lessons Learned\n\n- **Hash Tables** data structure;\n- Transforming a *O*(n²) into a *O*(n) solution;\n\n## :hammer: Technologies \u0026 Resources\n\n- *C* language\n- Minimalist GNU for Windows\n\n## :bell: Compiling and Running the Program\n\nTo execute the program, first you need to compile its source code using a **C compiler**. As I am on Windows, I'm using *[MinGW](http://www.mingw.org/)* that allows me to use `gcc` for compilation:\n\n```bash\n# Compiles the application to \"kc.exe\"\n$ gcc main.c -o kc\n\n# Run the application\n$ kc\n\n# Run the application using files as standard input\n$ kc \u003c test1.txt\n$ kc \u003c test2.txt\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliolmuller%2Fhash-k-complementary-problem","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuliolmuller%2Fhash-k-complementary-problem","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuliolmuller%2Fhash-k-complementary-problem/lists"}