{"id":5806,"date":"2019-11-08T08:48:38","date_gmt":"2019-11-08T16:48:38","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5806"},"modified":"2019-11-08T08:52:07","modified_gmt":"2019-11-08T16:52:07","slug":"leetcode-1250-check-if-it-is-a-good-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1250-check-if-it-is-a-good-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1250. Check If It Is a Good Array"},"content":{"rendered":"\n<p>Given an array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;positive integers. Your task is to select some subset of&nbsp;<code>nums<\/code>, multiply each element by an integer and add all these numbers.&nbsp;The array is said to be&nbsp;<strong>good&nbsp;<\/strong>if you can obtain a sum of&nbsp;<code>1<\/code>&nbsp;from the array by any possible subset and multiplicand.<\/p>\n\n\n\n<p>Return&nbsp;<code>True<\/code>&nbsp;if the array is&nbsp;<strong>good&nbsp;<\/strong>otherwise&nbsp;return&nbsp;<code>False<\/code>.<\/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> nums = [12,5,7,23]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Pick numbers 5 and 7.\n5*3 + 7*(-2) = 1\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> nums = [29,6,10]\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> Pick numbers 29, 6 and 10.\n29*1 + 6*(-3) + 10*(-1) = 1\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> nums = [3,6]\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 10^5<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Math<\/strong><\/h2>\n\n\n\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/B%C3%A9zout%27s_identity\">B\u00e9zout&#8217;s lemma<\/a>: b[1] * A[1] + b[2] * A[2] + &#8230;. + b[n] * A[n] = 1 &lt;==> gcd(A[1], A[2], &#8230;, A[n]) = 1 &lt;=> at least two numbers are co-prime<\/p>\n\n\n\n<p>Time complexity: O(n)<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  bool isGoodArray(vector<int>& nums) {\n    int g = nums[0];\n    for (int x : nums)\n      g = gcd(g, x);    \n    return g == 1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array&nbsp;nums&nbsp;of&nbsp;positive integers. Your task is to select some subset of&nbsp;nums, multiply each element by an integer and add all these numbers.&nbsp;The array is&#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":[359,217,31],"class_list":["post-5806","post","type-post","status-publish","format-standard","hentry","category-math","tag-gcd","tag-hard","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5806","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=5806"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5806\/revisions"}],"predecessor-version":[{"id":5808,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5806\/revisions\/5808"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}