{"id":22513831,"url":"https://github.com/emahtab/four-sum","last_synced_at":"2026-01-06T20:51:21.239Z","repository":{"id":79525522,"uuid":"232873513","full_name":"eMahtab/four-sum","owner":"eMahtab","description":"Four Sum","archived":false,"fork":false,"pushed_at":"2020-01-09T18:34:21.000Z","size":6,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-02T03:25:29.774Z","etag":null,"topics":["leetcode","problem-solving"],"latest_commit_sha":null,"homepage":null,"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}},"created_at":"2020-01-09T18:07:45.000Z","updated_at":"2020-01-09T18:34:24.000Z","dependencies_parsed_at":"2023-05-10T17:15:39.663Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/four-sum","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffour-sum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffour-sum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffour-sum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Ffour-sum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/four-sum/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245952040,"owners_count":20699420,"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":["leetcode","problem-solving"],"created_at":"2024-12-07T03:14:35.863Z","updated_at":"2026-01-06T20:51:21.198Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Four Sum\n## https://leetcode.com/problems/4sum\n\nGiven an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.\n\n**Note: The solution set must not contain duplicate quadruplets.**\n\n```\nExample:\n\nGiven array nums = [1, 0, -1, 0, -2, 2], and target = 0.\n\nA solution set is:\n[\n  [-1,  0, 0, 1],\n  [-2, -1, 1, 2],\n  [-2,  0, 0, 2]\n]\n```\n\n## Implementation :\n\n```java\npublic static List\u003cList\u003cInteger\u003e\u003e fourSum(int[] nums, int target) {\n        List\u003cList\u003cInteger\u003e\u003e result = new ArrayList\u003cList\u003cInteger\u003e\u003e();\n    \tif(nums == null || nums.length \u003c 4)\n        \treturn result;\n    \t\n    \tint n = nums.length; \n    \tfor(int i = 0; i \u003c n - 3; i++) {\n    \t    for(int j = i + 1; j \u003c n - 2; j++) {\n    \t\tfor(int k = j + 1; k \u003c n - 1; k++) {\n    \t\t    for(int l = k + 1; l \u003c n; l++) {\n    \t\t\t if((nums[i] + nums[j] + nums[k] + nums[l]) == target ) {\n        \t\t\taddResult(result, new Integer[] {nums[i], nums[j], nums[k], nums[l]});\n        \t\t\tbreak;\n        \t\t   }\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[] quadruplet) {\n\tSet\u003cInteger\u003e fourSum = new HashSet\u003cInteger\u003e(Arrays.asList(quadruplet));\n\tboolean alredayExists = false;\n\tfor(List\u003cInteger\u003e list: result) {\n\t\tSet\u003cInteger\u003e res = new HashSet\u003cInteger\u003e(list);\n\t\tif(res.equals(fourSum)) {\n\t\t    alredayExists = true;\n\t\t    break;\n\t\t}\n\t}\n\t\n\tif(!alredayExists) {\n\t   result.add(Arrays.asList(quadruplet));\n\t}\t\n }\n```\n\nThe runtime complexity of above implementation is O(n^4) and space complexity is O(1)\n```\nRuntime Complexity = O(n^4)\nSpace Complexity   = O(1)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffour-sum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Ffour-sum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Ffour-sum/lists"}