{"id":9355,"date":"2021-12-31T15:16:50","date_gmt":"2021-12-31T23:16:50","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9355"},"modified":"2021-12-31T15:17:24","modified_gmt":"2021-12-31T23:17:24","slug":"leetcode-1979-find-greatest-common-divisor-of-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1979-find-greatest-common-divisor-of-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1979. Find Greatest Common Divisor of Array"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>nums<\/code>, return<strong>&nbsp;<\/strong><em>the&nbsp;<strong>greatest common divisor<\/strong>&nbsp;of the smallest number and largest number in&nbsp;<\/em><code>nums<\/code>.<\/p>\n\n\n\n<p>The&nbsp;<strong>greatest common divisor<\/strong>&nbsp;of two numbers is the largest positive integer that evenly divides both numbers.<\/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 = [2,5,6,9,10]\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nThe smallest number in nums is 2.\nThe largest number in nums is 10.\nThe greatest common divisor of 2 and 10 is 2.\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 = [7,5,6,8,3]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong>\nThe smallest number in nums is 3.\nThe largest number in nums is 8.\nThe greatest common divisor of 3 and 8 is 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,3]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong>\nThe smallest number in nums is 3.\nThe largest number in nums is 3.\nThe greatest common divisor of 3 and 3 is 3.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution:<\/strong><\/h2>\n\n\n\n<p>Use <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/algorithm\/minmax_element\" target=\"_blank\" rel=\"noreferrer noopener\">std::minmax_element<\/a> and <a href=\"https:\/\/en.cppreference.com\/w\/cpp\/numeric\/gcd\" target=\"_blank\" rel=\"noreferrer noopener\">std::gcd<\/a><\/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  int findGCD(vector<int>& nums) {\n    auto p = minmax_element(begin(nums), end(nums));     \n    return gcd(*p.first, *p.second);\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;nums, return&nbsp;the&nbsp;greatest common divisor&nbsp;of the smallest number and largest number in&nbsp;nums. The&nbsp;greatest common divisor&nbsp;of two numbers is the largest positive integer that&#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":[222,359,31,758],"class_list":["post-9355","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-gcd","tag-math","tag-minmax_element","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9355","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=9355"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9355\/revisions"}],"predecessor-version":[{"id":9358,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9355\/revisions\/9358"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}