{"id":9542,"date":"2022-03-04T22:31:22","date_gmt":"2022-03-05T06:31:22","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9542"},"modified":"2022-03-04T22:34:37","modified_gmt":"2022-03-05T06:34:37","slug":"leetcode-2178-maximum-split-of-positive-even-integers","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2178-maximum-split-of-positive-even-integers\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2178. Maximum Split of Positive Even Integers"},"content":{"rendered":"\n<p>You are given an integer&nbsp;<code>finalSum<\/code>. Split it into a sum of a&nbsp;<strong>maximum<\/strong>&nbsp;number of&nbsp;<strong>unique<\/strong>&nbsp;positive even integers.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example, given&nbsp;<code>finalSum = 12<\/code>, the following splits are&nbsp;<strong>valid<\/strong>&nbsp;(unique positive even integers summing up to&nbsp;<code>finalSum<\/code>):&nbsp;<code>(12)<\/code>,&nbsp;<code>(2 + 10)<\/code>,&nbsp;<code>(2 + 4 + 6)<\/code>, and&nbsp;<code>(4 + 8)<\/code>. Among them,&nbsp;<code>(2 + 4 + 6)<\/code>&nbsp;contains the maximum number of integers. Note that&nbsp;<code>finalSum<\/code>&nbsp;cannot be split into&nbsp;<code>(2 + 2 + 4 + 4)<\/code>&nbsp;as all the numbers should be unique.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>a list of integers that represent a valid split containing a&nbsp;<strong>maximum<\/strong>&nbsp;number of integers<\/em>. If no valid split exists for&nbsp;<code>finalSum<\/code>, return&nbsp;<em>an&nbsp;<strong>empty<\/strong>&nbsp;list<\/em>. You may return the integers in&nbsp;<strong>any<\/strong>&nbsp;order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> finalSum = 12\n<strong>Output:<\/strong> [2,4,6]\n<strong>Explanation:<\/strong> The following are valid splits: <code>(12)<\/code>, <code>(2 + 10)<\/code>, <code>(2 + 4 + 6)<\/code>, and <code>(4 + 8)<\/code>.\n(2 + 4 + 6) has the maximum number of integers, which is 3. Thus, we return [2,4,6].\nNote that [2,6,4], [6,2,4], etc. are also accepted.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> finalSum = 7\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> There are no valid splits for the given finalSum.\nThus, we return an empty array.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> finalSum = 28\n<strong>Output:<\/strong> [6,8,2,12]\n<strong>Explanation:<\/strong> The following are valid splits: <code>(2 + 26)<\/code>, <code>(6 + 8 + 2 + 12)<\/code>, and <code>(4 + 24)<\/code>. \n<code>(6 + 8 + 2 + 12)<\/code> has the maximum number of integers, which is 4. Thus, we return [6,8,2,12].\nNote that [10,2,4,12], [6,2,4,16], etc. are also accepted.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= finalSum &lt;= 10<sup>10<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>The get the maximum number of elements, we must use the smallest numbers.<\/p>\n\n\n\n<p>[2, 4, 6, &#8230;, 2k, x], where x > 2k<br>let s = 2 + 4 + &#8230; + 2k, x = num &#8211; s<br>since num is odd and s is also odd, so thus x = num &#8211; s.<\/p>\n\n\n\n<p>Time complexity: O(sqrt(num)) for constructing outputs.<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<long long> maximumEvenSplit(long long finalSum) {\n    if (finalSum & 1) return {};\n    vector<long long> ans;\n    long long s = 0;\n    for (long long i = 2; s + i <= finalSum; s += i, i += 2)\n      ans.push_back(i);\n    ans.back() += (finalSum - s);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an integer&nbsp;finalSum. Split it into a sum of a&nbsp;maximum&nbsp;number of&nbsp;unique&nbsp;positive even integers. For example, given&nbsp;finalSum = 12, the following splits are&nbsp;valid&nbsp;(unique positive&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,31,177],"class_list":["post-9542","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-math","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9542","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=9542"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9542\/revisions"}],"predecessor-version":[{"id":9545,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9542\/revisions\/9545"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}