{"id":22513524,"url":"https://github.com/emahtab/three-sum","last_synced_at":"2026-02-10T22:05:18.887Z","repository":{"id":79525836,"uuid":"224479612","full_name":"eMahtab/three-sum","owner":"eMahtab","description":"Three Sum","archived":false,"fork":false,"pushed_at":"2025-01-16T02:57:00.000Z","size":25,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-14T12:37:26.700Z","etag":null,"topics":["3sum","leetocde","problem-solving"],"latest_commit_sha":null,"homepage":"","language":null,"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/eMahtab.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,"zenodo":null}},"created_at":"2019-11-27T17:06:38.000Z","updated_at":"2025-01-16T02:57:53.000Z","dependencies_parsed_at":"2025-04-14T12:40:37.750Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/three-sum","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eMahtab/three-sum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fthree-sum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fthree-sum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fthree-sum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fthree-sum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/three-sum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fthree-sum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29319312,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-10T20:44:44.282Z","status":"ssl_error","status_checked_at":"2026-02-10T20:44:43.393Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["3sum","leetocde","problem-solving"],"created_at":"2024-12-07T03:13:06.313Z","updated_at":"2026-02-10T22:05:18.869Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Three Sum\n## https://leetcode.com/problems/3sum\n\nGiven an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.\n\n**Note: The solution set must not contain duplicate triplets.**\n\n```\nExample:\n\nGiven array nums = [-1, 0, 1, 2, -1, -4],\n\nA solution set is:\n[\n  [-1, 0, 1],\n  [-1, -1, 2]\n]\n```\n\n## Implementation 1 : O(n^3) Time Limit Exceeded 😰\n\n```java\npublic static List\u003cList\u003cInteger\u003e\u003e threeSum(int[] nums) {\n        List\u003cList\u003cInteger\u003e\u003e result = new ArrayList\u003cList\u003cInteger\u003e\u003e();\n    \tif(nums == null || nums.length \u003c 3)\n        \treturn result;\n    \t\n    \tint n = nums.length; \n    \tfor(int i = 0; i \u003c n - 2; i++) {\n    \t    for(int j = i + 1; j \u003c n - 1; j++) {\n    \t\tfor(int k = j + 1; k \u003c n; k++) {\n    \t\t     if((nums[i] + nums[j] + nums[k]) == 0 ) {\n    \t\t\t  addResult(result, new Integer[] {nums[i], nums[j], nums[k]});\n    \t\t      }\n    \t\t}\n    \t    }\n    \t}\n    \t\n    \treturn result;\n}\n\nprivate static void addResult(List\u003cList\u003cInteger\u003e\u003e result, Integer[] triplet) {\n\tSet\u003cInteger\u003e threeSum = new HashSet\u003cInteger\u003e(Arrays.asList(triplet));\n\tboolean alredayExists = false;\n\tfor(List\u003cInteger\u003e list: result) {\n\t     Set\u003cInteger\u003e res = new HashSet\u003cInteger\u003e(list);\n\t     if(res.equals(threeSum)) {\n\t\t alredayExists = true;\n\t\t break;\n\t     }\n\t}\n\t\n\tif(!alredayExists) {\n\t   result.add(Arrays.asList(triplet));\n\t}\t\n}\n```\n\nAbove implementation have Runtime complexity of O(n^3) and space complexity of O(1)\n```\nRuntime Complexity = O(n^3)\nSpace Complexity   = O(1)\n```\n\n## Implementation 2 : O(n^2) Time Limit Exceeded 😰\n\n```java\npublic static List\u003cInteger[]\u003e threeSum(int[] nums) {\n        List\u003cInteger[]\u003e result = new ArrayList\u003cInteger[]\u003e();\n    \tif(nums == null || nums.length \u003c 3)\n        \treturn result;\n    \t\n    \tint n = nums.length; \n    \tArrays.sort(nums);\n    \tfor(int i = 0; i \u003c n - 2; i++) {\n    \t\tint left = i + 1;\n    \t\tint right = n - 1;\n    \t\twhile(left \u003c right) {\n    \t\t\tif( (nums[i] + nums[left] + nums[right]) \u003e 0) {\n    \t\t\t\tright --;\n    \t\t\t} else if( (nums[i] + nums[left] + nums[right]) \u003c 0) {\n    \t\t\t\tleft++;\n    \t\t\t} else {\n    \t\t\t\taddResult(result, new Integer[] {nums[i], nums[left], nums[right]});\n    \t\t\t\tleft++;\n    \t\t\t\tright--;\n    \t\t\t}\n    \t\t}\n    \t}\n    \t\n    \treturn result;\n    }\n\n\tprivate static void addResult(List\u003cInteger[]\u003e result, Integer[] triplet) {\n\t\tSet\u003cInteger\u003e threeSum = new HashSet\u003cInteger\u003e(Arrays.asList(triplet));\n\t\tboolean alredayExists = false;\n\t\tfor(Integer[] list: result) {\n\t\t\tSet\u003cInteger\u003e res = new HashSet\u003cInteger\u003e(Arrays.asList(list));\n\t\t\tif(res.equals(threeSum)) {\n\t\t\t\talredayExists = true;\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\t\tif(!alredayExists) {\n\t\t\tresult.add(triplet);\n\t\t}\n\t\t\n\t}\n```\nAbove implementation have Runtime complexity of O(n^2) and space complexity of O(1)\n```\nRuntime Complexity = O(n^2)\nSpace Complexity   = O(1)\n```\n\n## Implementation 3 : O(n^2) Optimization - Handling duplicates in beautiful way 😊\n\n```java\nclass Solution {\n    public List\u003cList\u003cInteger\u003e\u003e threeSum(int[] nums) {\n        List\u003cList\u003cInteger\u003e\u003e response = new ArrayList\u003c\u003e();\n        if(nums == null || nums.length \u003c 3)\n            return response;\n            \n        Arrays.sort(nums);\n        int n = nums.length;\n        for(int i = 0; i \u003c n-2; i++) {\n            if(i \u003e 0 \u0026\u0026 nums[i] == nums[i-1])\n                continue;\n            \n            int left = i+1;\n            int right = n-1;\n            while(left \u003c right) {\n                int sum = nums[i] + nums[left] + nums[right];\n                if(sum == 0) {\n                    response.add(Arrays.asList(nums[i], nums[left], nums[right]));\n                    while(left \u003c right \u0026\u0026 nums[left] == nums[left+1])\n                        left++;\n                    while(left \u003c right \u0026\u0026 nums[right] == nums[right-1])\n                        right--;\n                    left++;\n                    right--;\n                } else if(sum \u003e 0) {\n                    right--;\n                } else {\n                    left++;\n                }\n            }\n        }\n       return response; \n    }\n}\n```\nAbove implementation have Runtime complexity of O(n^2) and space complexity of O(1)\n```\nRuntime Complexity = O(n^2)\nSpace Complexity   = O(1)\n```\n**Note that, in above implementation, we are not using `addResult()` method to handle duplicate triplets, rather we are handling duplicate scenarios, when we are adding a triplet to the final result. This optimizes the runtime of the algorithm and makes it fast.**\n\n## References :\n1. https://massivealgorithms.blogspot.com/2014/06/leetcode-3sum.html\n2. https://www.youtube.com/watch?v=qJSPYnS35SE\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fthree-sum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fthree-sum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fthree-sum/lists"}