{"id":9538,"date":"2022-03-04T21:24:26","date_gmt":"2022-03-05T05:24:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9538"},"modified":"2022-03-04T21:26:19","modified_gmt":"2022-03-05T05:26:19","slug":"leetcode-2177-find-three-consecutive-integers-that-sum-to-a-given-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-2177-find-three-consecutive-integers-that-sum-to-a-given-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2177. Find Three Consecutive Integers That Sum to a Given Number"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>num<\/code>, return&nbsp;<em>three consecutive integers (as a sorted array)<\/em><em>&nbsp;that&nbsp;<strong>sum<\/strong>&nbsp;to&nbsp;<\/em><code>num<\/code>. If&nbsp;<code>num<\/code>&nbsp;cannot be expressed as the sum of three consecutive integers, return<em>&nbsp;an&nbsp;<strong>empty<\/strong>&nbsp;array.<\/em><\/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> num = 33\n<strong>Output:<\/strong> [10,11,12]\n<strong>Explanation:<\/strong> 33 can be expressed as 10 + 11 + 12 = 33.\n10, 11, 12 are 3 consecutive integers, so we return [10, 11, 12].\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> num = 4\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> There is no way to express 4 as the sum of 3 consecutive integers.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>0 &lt;= num &lt;= 10<sup>15<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p>(x \/ 3 &#8211; 1) + (x \/ 3) +  (x \/ 3 + 1) == 3x == num, num must be divisible by 3.<\/p>\n\n\n\n<p>Time complexity: O(1)<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> sumOfThree(long long num) {\n    if (num % 3) return {};\n    return {num \/ 3 - 1, num \/ 3, num \/ 3 + 1};    \n  }\n};<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;num, return&nbsp;three consecutive integers (as a sorted array)&nbsp;that&nbsp;sum&nbsp;to&nbsp;num. If&nbsp;num&nbsp;cannot be expressed as the sum of three consecutive integers, return&nbsp;an&nbsp;empty&nbsp;array. Example 1: Input: num&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31],"class_list":["post-9538","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9538","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=9538"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9538\/revisions"}],"predecessor-version":[{"id":9541,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9538\/revisions\/9541"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}