{"id":22513463,"url":"https://github.com/emahtab/combination-sum","last_synced_at":"2026-03-19T23:03:02.602Z","repository":{"id":259690754,"uuid":"863296741","full_name":"eMahtab/combination-sum","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-27T10:11:55.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:29.123Z","etag":null,"topics":["backtracking","combinations","leetcode"],"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}},"created_at":"2024-09-26T03:57:47.000Z","updated_at":"2024-10-27T10:11:58.000Z","dependencies_parsed_at":"2024-10-27T11:35:30.055Z","dependency_job_id":"a7614ce7-aa03-47e8-ac23-bb3ada26decf","html_url":"https://github.com/eMahtab/combination-sum","commit_stats":null,"previous_names":["emahtab/combination-sum"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/combination-sum/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950665,"owners_count":20699106,"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":["backtracking","combinations","leetcode"],"created_at":"2024-12-07T03:12:25.765Z","updated_at":"2026-01-07T12:04:39.399Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Combination Sum\n\n## https://leetcode.com/problems/combination-sum\n\nGiven an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.\n\nThe same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the \nfrequency\n of at least one of the chosen numbers is different.\n\nThe test cases are generated such that the number of unique combinations that sum up to target is less than 150 combinations for the given input.\n\n```\nExample 1:\n\nInput: candidates = [2,3,6,7], target = 7\nOutput: [[2,2,3],[7]]\nExplanation:\n2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.\n7 is a candidate, and 7 = 7.\nThese are the only two combinations.\n\nExample 2:\n\nInput: candidates = [2,3,5], target = 8\nOutput: [[2,2,2,2],[2,3,3],[3,5]]\n\nExample 3:\n\nInput: candidates = [2], target = 1\nOutput: []\n```\n## Approach :\n1. We keep trying to add the same number for as long as we can add (means if candidates[i] \u003c= target we will add the same element and keep trying, reducing the target to target - candidates[i),\n2. If the same number can't be added anymore we try with the next number, then the next one, till the very last last element. If none of these result in a solution, we remove the last element we added to the list. And try with the next element. This way we will search every possible combination that might lead to a solution. \n\n### Since we are asked to return unique combinations and given that candidates[i] is unique, we don't include the previous candidate when we start our search from index+1\n\n## Implementation : Find Combination and Backtrack when no solution possible\n```java\nclass Solution {\n    public List\u003cList\u003cInteger\u003e\u003e combinationSum(int[] candidates, int target) {\n        List\u003cList\u003cInteger\u003e\u003e result = new ArrayList\u003c\u003e();\n        if(candidates == null || candidates.length == 0)\n        return result;\n        findCombination(candidates, target, 0, new ArrayList\u003cInteger\u003e(), result);\n        return result;\n    }\n    private void findCombination(int[] candidates, int target, int index, List\u003cInteger\u003e combination,\n      List\u003cList\u003cInteger\u003e\u003e result) {\n        if(target == 0) {\n    \t    result.add(new ArrayList\u003cInteger\u003e(combination));\n    \t    return;\n    \t}\n    \tfor(int i = index; i \u003c candidates.length; i++) {\n    \t    if(candidates[i] \u003c= target) {\n    \t        combination.add(candidates[i]);\n    \t        if(target - candidates[i] \u003e= 0)\n    \t        \tfindCombination(candidates, target - candidates[i], i, combination, result);\n    \t        combination.remove(combination.size()-1);\n    \t    }   \n    \t}\n    }\n}\n```\n## Time and Space Complexity\nThe time complexity of the combinationSum function is exponential because for each element in the candidates array, the function makes a recursive call to itself with a reduced target value.\n### Time Complexity = O(2^N)\n\n### Space Complexity = O(N)\n\n# References :\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcombination-sum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcombination-sum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcombination-sum/lists"}