{"id":884,"date":"2025-08-11T17:28:44","date_gmt":"2025-08-11T11:58:44","guid":{"rendered":"https:\/\/codeanddebug.in\/blog\/?p=884"},"modified":"2025-08-11T17:29:02","modified_gmt":"2025-08-11T11:59:02","slug":"minimum-sum-partition","status":"publish","type":"post","link":"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/","title":{"rendered":"Minimum sum partition | Solved using Tabulation in Python"},"content":{"rendered":"\n<p>Given an array&nbsp;<strong>arr[]<\/strong>&nbsp;<strong>&nbsp;<\/strong>containing&nbsp;<strong>non-negative&nbsp;<\/strong>integers, the task is to divide it into two sets&nbsp;<strong>set1<\/strong>&nbsp;and&nbsp;<strong>set2<\/strong>&nbsp;such that the absolute difference between their sums is minimum and find the&nbsp;<strong>minimum<\/strong>&nbsp;difference.<\/p>\n\n\n\n<p>Here&#8217;s the [<strong><a href=\"https:\/\/www.geeksforgeeks.org\/problems\/minimum-sum-partition3317\/1\" 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>Examples:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input<\/strong>: arr[] = [1, 6, 11, 5]\n<strong>Output:<\/strong> 1\n<strong>Explanation<\/strong>: \nSubset1 = {1, 5, 6}, sum of Subset1 = 12 \nSubset2 = {11}, sum of Subset2 = 11 <br>Hence, minimum difference is 1.  <\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [1, 4]\n<strong>Output: <\/strong>3\n<strong>Explanation<\/strong>: \nSubset1 = {1}, sum of Subset1 = 1\nSubset2 = {4}, sum of Subset2 = 4<br>Hence, minimum difference is 3.<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input: <\/strong>arr[] = [1]\n<strong>Output: <\/strong>1\n<strong>Explanation<\/strong>: \nSubset1 = {1}, sum of Subset1 = 1\nSubset2 = {}, sum of Subset2 = 0<br>Hence, minimum difference is 1.<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><br>1 \u2264 arr.size()*|sum of array elements| \u2264 10<sup>5<\/sup><br>1 &lt;= arr[i] &lt;= 10<sup>5<\/sup><\/p>\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-ee43f87f-cba7-43c7-9806-3c939ae609d1\" 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\/minimum-sum-partition\/#0-tabulation-bottom-up-dp\" style=\"\">Tabulation (Bottom-Up DP)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#1-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#2-code-implementation\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#3-how-it-works\" style=\"\">How It Works<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#4-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#5-tabulation-space-optimization\" style=\"\">Tabulation (Space Optimization)<\/a><ul><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#6-intuition-and-approach\" style=\"\">Intuition and Approach<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#7-code-implementation-\" style=\"\">Code Implementation<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#8-how-it-works\" style=\"\">How It Works<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#9-time-and-space-complexity\" style=\"\">Time and Space Complexity<\/a><\/li><\/ul><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#10-practical-tips\" style=\"\">Practical Tips<\/a><\/li><li style=\"\"><a href=\"https:\/\/codeanddebug.in\/blog\/minimum-sum-partition\/#11-conclusion\" style=\"\">Conclusion<\/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-tabulation-bottom-up-dp\">Tabulation (Bottom-Up DP)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Build a boolean DP table dp[n][total+1], where dp[i][t] is True if a subset of nums[0..i] can achieve sum t.<\/li>\n\n\n\n<li>Initialize:\n<ul class=\"wp-block-list\">\n<li>dp[i] = True for all i (sum 0 achievable with empty subset).<\/li>\n\n\n\n<li>dp[nums] = True if within bounds.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Transition for each index and target:\n<ul class=\"wp-block-list\">\n<li>pick = dp[i\u22121][t \u2212 nums[i]] if nums[i] \u2264 t else False<\/li>\n\n\n\n<li>not_pick = dp[i\u22121][t]<\/li>\n\n\n\n<li>dp[i][t] = pick or not_pick<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>After filling, iterate over all achievable s1 and compute min |total \u2212 2*s1|.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2-code-implementation\">Code Implementation<\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro 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.5rem;--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 minDifference(self, nums):\n        n = len(nums)\n        total = sum(nums)\n        # dp[i][t] -&gt; can we make sum t using elements up to index i?\n        dp = [[False for _ in range(total + 1)] for _ in range(n)]\n        # Base: sum 0 is always achievable (empty subset)\n        for index in range(n):\n            dp[index] = True\n        # Seed with the first element if within range\n        if nums &lt;= total:\n            dp[nums] = True\n        # Fill DP table\n        for index in range(1, n):\n            for target in range(0, total + 1):\n                if nums[index] &gt; target:\n                    pick = False\n                else:\n                    pick = dp[index - 1][target - nums[index]]\n                not_pick = dp[index - 1][target]\n                dp[index][target] = pick or not_pick\n        # Sweep all achievable sums s1 to minimize |total - 2*s1|\n        min_diff = float(&quot;inf&quot;)\n        for s1 in range(0, total + 1):\n            if dp[n - 1][s1] == True:\n                s2 = total - s1\n                min_diff = min(min_diff, abs(s1 - s2))\n        return min_diff\" 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\">minDifference<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        total = <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># dp[i][t] -&gt; can we make sum t using elements up to index i?<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        dp = [[<\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)] <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Base: sum 0 is always achievable (empty subset)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> index <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            dp[index] = <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Seed with the first element if within range<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums &lt;= total:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            dp[nums] = <\/span><span style=\"color: #569CD6\">True<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Fill DP table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> index <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> target <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[index] &gt; target:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    pick = <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    pick = dp[index - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">][target - nums[index]]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                not_pick = dp[index - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">][target]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                dp[index][target] = pick <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> not_pick<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Sweep all achievable sums s1 to minimize |total - 2*s1|<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        min_diff = <\/span><span style=\"color: #4EC9B0\">float<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;inf&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> s1 <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> dp[n - <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">][s1] == <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                s2 = total - s1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                min_diff = <\/span><span style=\"color: #DCDCAA\">min<\/span><span style=\"color: #D4D4D4\">(min_diff, <\/span><span style=\"color: #DCDCAA\">abs<\/span><span style=\"color: #D4D4D4\">(s1 - s2))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> min_diff<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3-how-it-works\">How It Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The DP table captures all reachable subset sums using the first i elements.<\/li>\n\n\n\n<li>Once populated, every True at dp[n\u22121][s1] indicates a candidate split S1\/S2.<\/li>\n\n\n\n<li>The best split is the one minimizing |total \u2212 2\u00b7s1|.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time<\/strong>: O(n \u00d7 total) &#8211; standard subset-sum DP.<\/li>\n\n\n\n<li><strong>Space<\/strong>: O(n \u00d7 total) &#8211; full table.<\/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=\"5-tabulation-space-optimization\">Tabulation (Space Optimization)<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6-intuition-and-approach\">Intuition and Approach<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each row depends only on the previous row, so we can reduce memory to two 1D arrays (prev and curr).<\/li>\n\n\n\n<li>The transition remains the same; we just roll arrays per element.<\/li>\n\n\n\n<li>After processing all elements, prev[t] tells if sum t is achievable.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7-code-implementation-\">Code Implementation <\/h3>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro 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.5rem;--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 minDifference(self, nums):\n        n = len(nums)\n        total = sum(nums)\n        # prev[t] -&gt; achievable sum t using elements up to previous index\n        prev = [False for _ in range(total + 1)]\n        prev[0] = True  # sum 0 is always achievable\n        if nums &lt;= total:\n            prev[nums] = True  # seed with first element\n        # Build achievable sums iteratively\n        for index in range(1, n):\n            curr = [False for _ in range(total + 1)]\n            for target in range(0, total + 1):\n                if nums[index] &gt; target:\n                    pick = False\n                else:\n                    pick = prev[target - nums[index]]\n                not_pick = prev[target]\n                curr[target] = pick or not_pick\n            prev = curr  # roll the arrays\n        # Scan all achievable s1 to minimize |total - 2*s1|\n        min_diff = float(&quot;inf&quot;)\n        for s1 in range(0, total + 1):\n            if prev[s1] == True:\n                s2 = total - s1\n                min_diff = min(min_diff, abs(s1 - s2))\n        return min_diff\" 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\">minDifference<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #9CDCFE\">self<\/span><span style=\"color: #D4D4D4\">, <\/span><span style=\"color: #9CDCFE\">nums<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        n = <\/span><span style=\"color: #DCDCAA\">len<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        total = <\/span><span style=\"color: #DCDCAA\">sum<\/span><span style=\"color: #D4D4D4\">(nums)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># prev[t] -&gt; achievable sum t using elements up to previous index<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        prev = [<\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        prev[<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">] = <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># sum 0 is always achievable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums &lt;= total:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            prev[nums] = <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">  <\/span><span style=\"color: #6A9955\"># seed with first element<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Build achievable sums iteratively<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> index <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">, n):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            curr = [<\/span><span style=\"color: #569CD6\">False<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> _ <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">)]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> target <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> nums[index] &gt; target:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    pick = <\/span><span style=\"color: #569CD6\">False<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                <\/span><span style=\"color: #C586C0\">else<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                    pick = prev[target - nums[index]]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                not_pick = prev[target]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                curr[target] = pick <\/span><span style=\"color: #569CD6\">or<\/span><span style=\"color: #D4D4D4\"> not_pick<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            prev = curr  <\/span><span style=\"color: #6A9955\"># roll the arrays<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #6A9955\"># Scan all achievable s1 to minimize |total - 2*s1|<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        min_diff = <\/span><span style=\"color: #4EC9B0\">float<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #CE9178\">&quot;inf&quot;<\/span><span style=\"color: #D4D4D4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">for<\/span><span style=\"color: #D4D4D4\"> s1 <\/span><span style=\"color: #C586C0\">in<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">range<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #B5CEA8\">0<\/span><span style=\"color: #D4D4D4\">, total + <\/span><span style=\"color: #B5CEA8\">1<\/span><span style=\"color: #D4D4D4\">):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            <\/span><span style=\"color: #C586C0\">if<\/span><span style=\"color: #D4D4D4\"> prev[s1] == <\/span><span style=\"color: #569CD6\">True<\/span><span style=\"color: #D4D4D4\">:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                s2 = total - s1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">                min_diff = <\/span><span style=\"color: #DCDCAA\">min<\/span><span style=\"color: #D4D4D4\">(min_diff, <\/span><span style=\"color: #DCDCAA\">abs<\/span><span style=\"color: #D4D4D4\">(s1 - s2))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> min_diff<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8-how-it-works\">How It Works<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The rolling arrays preserve correctness while cutting space from O(n\u00d7total) to O(total).<\/li>\n\n\n\n<li>The final pass over achievable sums finds the minimal difference.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9-time-and-space-complexity\">Time and Space Complexity<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Time<\/strong>: O(n \u00d7 total) &#8211; same as full DP.<\/li>\n\n\n\n<li><strong>Space<\/strong>: O(total) &#8211; memory-optimized with rolling arrays.<\/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=\"10-practical-tips\">Practical Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>You can restrict the final sweep to s1 in [0..total\/\/2] since differences mirror around half the total, but the provided code correctly checks all sums for simplicity.<\/li>\n\n\n\n<li>This DP pattern is a direct extension of\u00a0<strong>Subset Sum<\/strong>\u00a0and closely related to\u00a0<strong>Partition Equal Subset Sum<\/strong>; all share the same pick\/not-pick transitions.<\/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=\"11-conclusion\">Conclusion<\/h2>\n\n\n\n<p>The Minimum Sum Partition problem reduces to computing all achievable subset sums and selecting the one closest to half the total. The&nbsp;<strong>tabulation<\/strong>&nbsp;approach is clear and robust, while the&nbsp;<strong>space-optimized<\/strong>&nbsp;version is preferred for tight memory limits. Both run in&nbsp;<strong>O(n \u00d7 total)<\/strong>&nbsp;time, making them practical for common constraints.<\/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&nbsp;arr[]&nbsp;&nbsp;containing&nbsp;non-negative&nbsp;integers, the task is to divide it into two sets&nbsp;set1&nbsp;and&nbsp;set2&nbsp;such that the absolute difference between their sums is minimum and find the&nbsp;minimum&nbsp;difference. Here&#8217;s the [Problem Link] to begin with. Examples: Input: arr[] = [1, 6, 11, 5] Output: 1 Explanation: Subset1 = {1, 5, 6}, sum of Subset1 = 12 Subset2 = {11},<\/p>\n","protected":false},"author":1,"featured_media":885,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ub_ctt_via":"","footnotes":""},"categories":[3,5],"tags":[39,18],"class_list":{"0":"post-884","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-structures-and-algorithm","8":"category-expert","9":"tag-dynamic-programming-on-subsequence","10":"tag-hard"},"featured_image_src":"https:\/\/codeanddebug.in\/blog\/wp-content\/uploads\/2025\/08\/minimum-sum-partition-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\/884","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=884"}],"version-history":[{"count":2,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/884\/revisions"}],"predecessor-version":[{"id":888,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/posts\/884\/revisions\/888"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media\/885"}],"wp:attachment":[{"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/media?parent=884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/categories?post=884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeanddebug.in\/blog\/wp-json\/wp\/v2\/tags?post=884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}