{"id":222,"date":"2026-11-13T12:35:02","date_gmt":"2026-11-13T04:35:02","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-meet-in-the-middle-splits-a-subset-sum\/"},"modified":"2026-11-13T21:53:06","modified_gmt":"2026-11-13T13:53:06","slug":"how-meet-in-the-middle-splits-a-subset-sum","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-meet-in-the-middle-splits-a-subset-sum\/","title":{"rendered":"How meet-in-the-middle splits a subset sum"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Meet-in-the-middle<\/strong> solves subset sum by splitting the items into two halves. Each half has about n\/2 elements, so each half has 2^{n\/2} subset sums. Sort one list. For every sum on the other list, binary search the complement that comes closest to the target without passing it. The time is <strong>O(n \u00b7 2^{n\/2})<\/strong> and the memory is <strong>\u0398(2^{n\/2})<\/strong>. The dynamic program is faster when the target is small. This split is faster when the target is huge and n is around 40.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Subset sum asks whether some subset of n integers adds to a target T, or asks for the largest sum that does not exceed T. The knapsack dynamic program runs in time proportional to n times the target. A target with hundreds of bits makes that table impossible. Enumerating every subset is 2^n, which stops being reasonable a little past n = 30 on ordinary machines.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Splitting the instance in half turns one exponential into two square roots. The left subsets and the right subsets can be listed completely. A sum that uses items from both sides is a pair. Searching the pair space explicitly would square the lists. Sorting one list and probing it turns the pair test into a binary search.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The search returns the exact optimum of this subset-sum instance: the largest subset sum that is at most T, or a report that none exists if that is the question. Every subset is a left subset plus a right subset, including the empty halves, so no combination is skipped. Time is dominated by building and sorting two lists of size 2^{n\/2}, which is O(n \u00b7 2^{n\/2}) arithmetic on integers of the input size. Extra memory is one of those lists.<\/p>\n\n\n<p class=\"wp-block-paragraph\">A 0-1 knapsack with a separate value on each item can use the same split if each half stores pairs of weight and value and discards dominated pairs. The list can then be shorter than 2^{n\/2}, and it is never longer. The plain sum problem does not need that filter. Negative numbers are allowed if the goal is restated. The usual statement uses positive integers and a sum cap.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Split the n items into two sets as equal as possible.<\/li>\n<li>Enumerate every subset sum of the left set and every subset sum of the right set. Include 0.<\/li>\n<li>Sort the right-hand sums.<\/li>\n<li>For each left sum a that is at most T, binary search the largest right sum that is at most T \u2212 a. Track the best total.<\/li>\n<li>A decision question stops early when a total equals T. An optimization question finishes the scan.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The exponential is 2^{n\/2}, which is still exponential. A target T that fits in a table is the 0-1 knapsack recurrence, linear in nT and polynomial in the numeric value. Meet-in-the-middle is the exact algorithm for a moderate number of items and a target too large to tabulate.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when both halves are enumerated and then nested naively<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Pair every left sum with every right sum in a double loop<\/td>\n<td>The answer is correct and the time returns to \u0398(2^n). The sort and the binary search are the whole saving.<\/td>\n<\/tr>\n<tr>\n<td>Drop the empty subset<\/td>\n<td>A solution that lies entirely in one half uses the empty sum on the other side. Leaving out 0 misses it.<\/td>\n<\/tr>\n<tr>\n<td>Replace the split with the knapsack table when T has hundreds of bits<\/td>\n<td>The table has a column per unit of capacity. It cannot be allocated. The split depends on n, not on T.<\/td>\n<\/tr>\n<tr>\n<td>Keep only the sums that equal T in each half<\/td>\n<td>A feasible pair can be 40 plus T minus 40, with neither half equal to T. Every sum up to T has to stay in the lists.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A capacity table is 0-1 knapsack. A greedy density scan is fractional knapsack and allows cutting items. Meet-in-the-middle is the exact half-enumeration for subset sum.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment splits the items in half, sorts one list of subset sums, and binary-searches the complement. The target 10 is reachable, so it prints <code>yes<\/code>.<\/p>\n\n<pre><code class=\"language-python\">import bisect\nnums = [3, 5, 7, 9, 1, 2]\ntarget = 10\nmid = len(nums) \/\/ 2\nleft, right = nums[:mid], nums[mid:]\n\ndef sums(group):\n    out = [0]\n    for x in group:\n        out += [s + x for s in out]\n    return out\n\nL, R = sums(left), sorted(sums(right))\nfound = False\nfor a in L:\n    if a &gt; target:\n        continue\n    j = bisect.bisect_right(R, target - a) - 1\n    if j &gt;= 0 and a + R[j] == target:\n        found = True\nassert found\nprint(&quot;yes&quot; if found else &quot;no&quot;)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Meet-in-the-middle splits a subset-sum or a knapsack of about forty items into two halves a computer can enumerate. The same split is why cascading a cipher twice does not double the key search. Bidirectional search is the graph form of the idea.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop the split when nT fits in memory and time. The dynamic program is then faster and uses less memory. Keep the split when n is near 40 and the target is far larger than 2^{n\/2}.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for meet-in-the-middle subset sum returns the two lists of size 2^{n\/2} and the binary search that joins them. <strong>Meet-in-the-middle solves subset sum exactly by enumerating each half, sorting one list of sums, and binary searching the complement of every sum from the other half.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Meet-in-the-middle solves subset sum by splitting the items into two halves. Each half has about n\/2 elements, so each half has 2^{n\/2} subset sums. Sort one list. For every sum on the other list, binary search the complement that comes closest to the target without passing it.&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,59],"tags":[],"class_list":["post-222","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-exact-algorithms"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/222","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=222"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions"}],"predecessor-version":[{"id":621,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/222\/revisions\/621"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=222"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=222"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=222"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}