{"id":748,"date":"2025-07-22T13:38:14","date_gmt":"2025-07-22T08:08:14","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=748"},"modified":"2025-07-22T13:38:15","modified_gmt":"2025-07-22T08:08:15","slug":"combination-sum","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/combination-sum\/","title":{"rendered":"Combination Sum | Leetcode 39 | Backtracking Approach"},"content":{"rendered":"\n<p>Given an array of&nbsp;<strong>distinct<\/strong>&nbsp;integers&nbsp;<code>candidates<\/code>&nbsp;and a target integer&nbsp;<code>target<\/code>, return&nbsp;<em>a list of all&nbsp;<strong>unique combinations<\/strong>&nbsp;of&nbsp;<\/em><code>candidates<\/code><em>&nbsp;where the chosen numbers sum to&nbsp;<\/em><code>target<\/code><em>.<\/em>&nbsp;You may return the combinations in&nbsp;<strong>any order<\/strong>.<\/p>\n\n\n\n<p>The&nbsp;<strong>same<\/strong>&nbsp;number may be chosen from&nbsp;<code>candidates<\/code>&nbsp;an&nbsp;<strong>unlimited number of times<\/strong>. Two combinations are unique if the&nbsp;frequency&nbsp;of at least one of the chosen numbers is different.<\/p>\n\n\n\n<p>The test cases are generated such that the number of unique combinations that sum up to&nbsp;<code>target<\/code>&nbsp;is less than&nbsp;<code>150<\/code>&nbsp;combinations for the given input.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/leetcode.com\/problems\/combination-sum\/description\/\" target=\"_blank\" rel=\"noreferrer noopener\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-purple-color\"><span style=\"text-decoration: underline;\">Problem Link<\/span><\/mark><\/a><\/strong>] to begin with.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> candidates = [2,3,6,7], target = 7<br><strong>Output:<\/strong> [[2,2,3],[7]]<br><strong>Explanation:<\/strong><br>2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.<br>7 is a candidate, and 7 = 7.<br>These are the only two combinations.<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> candidates = [2,3,5], target = 8<br><strong>Output:<\/strong> [[2,2,2,2],[2,3,3],[3,5]]<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> candidates = [2], target = 1<br><strong>Output:<\/strong> []<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>1 &lt;= candidates.length &lt;= 30<\/code><\/li>\n\n\n\n<li><code>2 &lt;= candidates[i] &lt;= 40<\/code><\/li>\n\n\n\n<li>All elements of\u00a0<code>candidates<\/code>\u00a0are\u00a0<strong>distinct<\/strong>.<\/li>\n\n\n\n<li><code>1 &lt;= target &lt;= 40<\/code><\/li>\n<\/ul>\n\n\n<div style=\"max-width: -moz-fit-content; \" class=\"wp-block-ub-table-of-contents-block ub_table-of-contents ub_table-of-contents-collapsed\" id=\"ub_table-of-contents-54043474-2452-42b2-a3ef-f3f5b2e195f2\" data-linktodivider=\"false\" data-showtext=\"show\" data-hidetext=\"hide\" data-scrolltype=\"auto\" data-enablesmoothscroll=\"true\" data-initiallyhideonmobile=\"true\" data-initiallyshow=\"false\"><div class=\"ub_table-of-contents-header-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-header\" style=\"text-align: left; \">\n\t\t\t\t<div class=\"ub_table-of-contents-title\">Contents:<\/div>\n\t\t\t\t<div class=\"ub_table-of-contents-header-toggle\">\n\t\t\t<div class=\"ub_table-of-contents-toggle\" style=\"\">\n\t\t\t\u00a0[<a class=\"ub_table-of-contents-toggle-link\" href=\"#\" style=\"\">show<\/a>]\n\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t<\/div>\n\t\t<\/div><div class=\"ub_table-of-contents-extra-container\" style=\"\">\n\t\t\t<div class=\"ub_table-of-contents-container ub_table-of-contents-1-column ub-hide\">\n\t\t\t\t<ul style=\"\"><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#0-solution-backtracking-approach\" style=\"\">Solution: Backtracking Approach<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#1-intuition\" style=\"\">Intuition<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#2-detailed-approach\" style=\"\">Detailed Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#3-code\" style=\"\">Code<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#4-code-explanation\" style=\"\">Code Explanation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#5-dry-run\" style=\"\">Dry Run<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#6-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/combination-sum\/#7-simplifying-it\" style=\"\">Simplifying It<\/a><\/li><\/ul>\n\t\t\t<\/div>\n\t\t<\/div><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-solution-backtracking-approach\">Solution: Backtracking Approach<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition\">Intuition<\/h3>\n\n\n\n<p>Think of this like making change for a specific amount using unlimited coins of certain denominations! You can use each coin type (candidate) as many times as you want. For each coin, you have two choices: take it (and you can take it again later) or skip to the next coin type. It&#8217;s like standing at a vending machine where you keep pressing the same button until you either reach your target amount or go over it. We use backtracking to explore all possible combinations, when we take a coin, we stay at the same position to allow taking it again, and when we skip, we move to the next coin type.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-detailed-approach\">Detailed Approach<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Recursive Function<\/strong>: Create a helper function that takes current index, running total, current subset, candidates array, target, and result list.<\/li>\n\n\n\n<li><strong>Base Cases<\/strong>:\n<ul class=\"wp-block-list\">\n<li>If total equals target: we found a valid combination, add a copy to results.<\/li>\n\n\n\n<li>If total exceeds target: this path won&#8217;t work, stop exploring.<\/li>\n\n\n\n<li>If index reaches array length: no more candidates to try, stop.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Choice 1 &#8211; Include Current Candidate<\/strong>: Add the candidate to subset and total, but keep the same index (allowing reuse of the same number).<\/li>\n\n\n\n<li><strong>Backtrack<\/strong>: After exploring the &#8220;include&#8221; path, remove the candidate from subset to clean up.<\/li>\n\n\n\n<li><strong>Choice 2 &#8211; Skip Current Candidate<\/strong>: Move to the next index without adding the current candidate.<\/li>\n\n\n\n<li><strong>Start Recursion<\/strong>: Begin with index 0, total 0, and empty subset.<\/li>\n\n\n\n<li><strong>Collect Results<\/strong>: All valid combinations will be stored in the result list.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-code\">Code<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#D4D4D4;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution:\n    def solve(self, index, total, subset, nums, target, result):\n        # Base case: If the running total matches the target, record the current subset\n        if total == target:\n            result.append(subset.copy())  # Add a copy to avoid future modifications\n            return\n        # Pruning: If the running total exceeds the target, stop this path\n        elif total &gt; target:\n            return\n        # Base case: If we have considered all candidates, terminate this branch\n        if index &gt;= len(nums):\n            return\n\n        # Choice 1: Include the candidate at the current index (allowing reuse)\n        Sum = total + nums[index]\n        subset.append(nums[index])\n        self.solve(index, Sum, subset, nums, target, result)  # Same index for reuse\n\n        # Backtrack: Remove the candidate just added\n        Sum = total  # Reset Sum to the value before including the candidate\n        subset.pop()\n\n        # Choice 2: Skip the candidate and move to the next index\n        self.solve(index + 1, Sum, subset, nums, target, result)\n\n    def combinationSum(self, candidates: List[int], target: int) -&gt; List[List[int]]:\n        result = []\n        self.solve(0, 0, [], candidates, target, result)\n        return result\" style=\"color:#D4D4D4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">solve<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">index<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">total<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">subset<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">target<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">result<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Base case: If the running total matches the target, record the current subset<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> total == target:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            result.append(subset.copy())  <\/span><span style=\"color: #6A9955\"># Add a copy to avoid future modifications<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Pruning: If the running total exceeds the target, stop this path<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">elif<\/span><span style=\"color: #D4D4D4\"> total &gt; target:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Base case: If we have considered all candidates, terminate this branch<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> index &gt;= <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">return<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Choice 1: Include the candidate at the current index (allowing reuse)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        Sum = total + nums[index]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        subset.append(nums[index])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(index, Sum, subset, nums, target, result)  <\/span><span style=\"color: #6A9955\"># Same index for reuse<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Backtrack: Remove the candidate just added<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        Sum = total  <\/span><span style=\"color: #6A9955\"># Reset Sum to the value before including the candidate<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        subset.pop()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Choice 2: Skip the candidate and move to the next index<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(index + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, Sum, subset, nums, target, result)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">def<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">combinationSum<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">candidates<\/span><span style=\"color: #D4D4D4\">: List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">], <\/span><span style=\"color: #9CDCFE\">target<\/span><span style=\"color: #D4D4D4\">: <\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">) -&gt; List[List[<\/span><span style=\"color: #4EC9B0\">int<\/span><span style=\"color: #D4D4D4\">]]:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        result = []<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">self<\/span><span style=\"color: #D4D4D4\">.solve(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, [], candidates, target, result)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> result<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">Python<\/span><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-code-explanation\">Code Explanation<\/h3>\n\n\n\n<p>The&nbsp;<code>solve<\/code>&nbsp;function explores all possible combinations using backtracking. The key insight is in the recursive calls: when we include a candidate, we call&nbsp;<code>self.solve(index, Sum, ...)<\/code>&nbsp;with the same index, allowing us to use the same number again. When we skip a candidate, we call&nbsp;<code>self.solve(index + 1, Sum, ...)<\/code>&nbsp;to move to the next number. The backtracking step (<code>subset.pop()<\/code>) ensures we clean up after trying each choice, so other branches start with a fresh state. We use&nbsp;<code>subset.copy()<\/code>&nbsp;when adding to results to prevent future modifications from affecting already stored combinations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5-dry-run\">Dry Run<\/h3>\n\n\n\n<p>Let&#8217;s trace through:&nbsp;<code>candidates =&nbsp;<\/code>, target = 7<\/p>\n\n\n\n<p><strong>Start: index=0, total=0, subset=[]<\/strong><\/p>\n\n\n\n<p><strong>Try including 2 (index=0):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>subset=, total=2, index=0 (stay at same position)<strong>Try including 2 again:<\/strong><ul><li>subset=, total=4, index=0<strong>Try including 2 again:<\/strong><ul><li>subset=, total=6, index=0<strong>Try including 2 again:<\/strong><ul><li>total=8 > 7, prune this path<\/li><\/ul><strong>Skip 2, try 3:<\/strong><ul><li>subset=, total=6, index=1<ul><li>subset=, total=9 > 7, prune<\/li><li>Skip 3: index=2 >= len(nums), return<\/li><\/ul><\/li><\/ul><\/li><\/ul><strong>Skip 2, try 3:<\/strong><ul><li>subset=, total=4, index=1<ul><li>subset=, total=7 = target \u2192 add to result<\/li><li>Skip 3: index=2 >= len(nums), return<\/li><\/ul><\/li><\/ul><\/li><\/ul><strong>Skip 2, try 3:<\/strong>\n<ul class=\"wp-block-list\">\n<li>subset=, total=2, index=1\n<ul class=\"wp-block-list\">\n<li>subset=, total=5, index=1\n<ul class=\"wp-block-list\">\n<li>subset=, total=8 > 7, prune<\/li>\n\n\n\n<li>Skip 3: index=2 >= len(nums), return<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Skip 3: index=2 >= len(nums), return<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Skip 2, try 3 (index=1):<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>subset=[], total=0, index=1\n<ul class=\"wp-block-list\">\n<li>subset=, total=3, index=1\n<ul class=\"wp-block-list\">\n<li>subset=, total=6, index=1\n<ul class=\"wp-block-list\">\n<li>subset=, total=9 > 7, prune<\/li>\n\n\n\n<li>Skip 3: index=2 >= len(nums), return<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Skip 3: index=2 >= len(nums), return<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Skip 3: index=2 >= len(nums), return<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p><strong>Result:<\/strong>&nbsp;<code>[]<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time Complexity:<\/strong>\u00a0O(N^(T\/M)) &#8211; Where N is the number of candidates, T is the target, and M is the minimal value among candidates. In worst case, we might have T\/M levels in recursion tree.<\/li>\n\n\n\n<li><strong>Space Complexity:<\/strong>\u00a0O(T\/M) &#8211; The recursion stack depth can go up to T\/M levels (when we keep choosing the smallest candidate).<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"7-simplifying-it\">Simplifying It<\/h2>\n\n\n\n<p>This backtracking approach is like trying to make exact change using unlimited coins. At each step, you decide: &#8220;Should I take this coin again, or should I move to the next type of coin?&#8221; The beauty is that you can keep taking the same coin until you either hit your target or go over it. The backtracking ensures you try all possible combinations systematically, you explore one path completely, then backtrack and try another path. The key difference from other subset problems is that here we can reuse elements, which is why we don&#8217;t increment the index when including a candidate.<\/p>\n\n\n\n<p>This problem is perfect for understanding how backtracking can handle problems with repetition allowed, and it&#8217;s a common interview question that tests your ability to handle recursive choices with state management!<\/p>\n\n\n\n<div class=\"wp-block-buttons is-content-justification-center is-layout-flex wp-container-core-buttons-is-layout-16018d1d wp-block-buttons-is-layout-flex\">\n<div class=\"wp-block-button\"><a class=\"wp-block-button__link wp-element-button\" href=\"https:\/\/codeanddebug.in\/course\/zero-to-hero-python-dsa\" target=\"_blank\" rel=\"noreferrer noopener\">Join our Advance DSA COURSE<\/a><\/div>\n<\/div>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>For any changes to the article, kindly email at <a href=\"mailto:code@codeanddebug.in\" target=\"_blank\" rel=\"noreferrer noopener\">code@codeanddebug.in<\/a> or contact us at <a href=\"tel:+91-9712928220\" target=\"_blank\" rel=\"noreferrer noopener\">+91-9712928220<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of&nbsp;distinct&nbsp;integers&nbsp;candidates&nbsp;and a target integer&nbsp;target, return&nbsp;a list of all&nbsp;unique combinations&nbsp;of&nbsp;candidates&nbsp;where the chosen numbers sum to&nbsp;target.&nbsp;You may return the combinations in&nbsp;any order. The&nbsp;same&nbsp;number may be chosen from&nbsp;candidates&nbsp;an&nbsp;unlimited number of times. Two combinations are unique if the&nbsp;frequency&nbsp;of at least one of the chosen numbers is different. The test cases are generated such that the number<\/p>\n","protected":false},"author":1,"featured_media":749,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,6],"tags":[32,19],"class_list":{"0":"post-748","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-intermediate","9":"tag-advance-recursion","10":"tag-medium"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/07\/combination-sum-featured-image.png","author_info":{"display_name":"codeanddebug","author_link":"https:\/\/codeanddebug.in\/blog\/author\/codeanddebug\/"},"_links":{"self":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/748","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/comments?post=748"}],"version-history":[{"count":1,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/748\/revisions"}],"predecessor-version":[{"id":750,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/748\/revisions\/750"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/749"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=748"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=748"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=748"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}