{"id":22513460,"url":"https://github.com/emahtab/combination-sum-ii","last_synced_at":"2026-03-19T23:02:41.204Z","repository":{"id":259690932,"uuid":"863436145","full_name":"eMahtab/combination-sum-ii","owner":"eMahtab","description":null,"archived":false,"fork":false,"pushed_at":"2024-10-27T10:14:07.000Z","size":14,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T03:19:27.191Z","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-26T09:39:56.000Z","updated_at":"2024-10-27T11:38:43.000Z","dependencies_parsed_at":"2024-10-27T11:54:06.474Z","dependency_job_id":null,"html_url":"https://github.com/eMahtab/combination-sum-ii","commit_stats":null,"previous_names":["emahtab/combination-sum-ii"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum-ii","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum-ii/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum-ii/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eMahtab%2Fcombination-sum-ii/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eMahtab","download_url":"https://codeload.github.com/eMahtab/combination-sum-ii/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245950659,"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.057Z","updated_at":"2026-01-07T11:36:51.540Z","avatar_url":"https://github.com/eMahtab.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Combination Sum II\n\nhttps://leetcode.com/problems/combination-sum-ii\n\nGiven a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.\n\nEach number in candidates may only be used once in the combination.\n\nNote: The solution set must not contain duplicate combinations.\n\n```\nExample 1:\n\nInput: candidates = [10,1,2,7,6,1,5], target = 8\nOutput: \n[\n[1,1,6],\n[1,2,5],\n[1,7],\n[2,6]\n]\n\nExample 2:\n\nInput: candidates = [2,5,2,1,2], target = 5\nOutput: \n[\n[1,2,2],\n[5]\n]\n```\n\n### Constraints:\n\n1. 1 \u003c= candidates.length \u003c= 100\n2. 1 \u003c= candidates[i] \u003c= 50\n3. 1 \u003c= target \u003c= 30\n\n## Approach :\nThis problem is similar to [Combination Sum](https://github.com/eMahtab/combination-sum) problem, with couple of conditions, a number n can only be used x times, where x is number of times n appears in candidates array. And we should not add duplicate combinations. To solve this problem we sort the candidates array and then find the combinations, we skip the processing if we encounter same number which is not the starting of the group of same numbers.\n\n## Implementation : \n```java\nclass Solution {\n    public List\u003cList\u003cInteger\u003e\u003e combinationSum2(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        Arrays.sort(candidates);\n        findCombination(candidates, target, 0, new ArrayList\u003cInteger\u003e(), result);\n        return result;\n    }\n\n    private void findCombination(int[] candidates, int target, int index, List\u003cInteger\u003e combination,\n  \t      List\u003cList\u003cInteger\u003e\u003e result) {\n  \t if(target == 0) {\n  \t    result.add(new ArrayList\u003cInteger\u003e(combination));\n  \t    return;\n  \t }\n  \t for(int i = index; i \u003c candidates.length; i++) {\n  \t    if(i \u003e index \u0026\u0026 candidates[i] == candidates[i-1]) continue; \n  \t\n  \t    if(candidates[i] \u003c= target) {\n  \t        combination.add(candidates[i]);\n  \t        if(target - candidates[i] \u003e= 0)\n  \t          findCombination(candidates, target - candidates[i], i+1, 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 combinationSum2 function is exponential because for each element in the candidates array, the function makes a recursive call.\n\n### Time Complexity = O(2^N)\n\n### Space Complexity = O(N)\n\n# References :\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcombination-sum-ii","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Femahtab%2Fcombination-sum-ii","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Femahtab%2Fcombination-sum-ii/lists"}