{"id":9561,"date":"2022-03-08T04:18:45","date_gmt":"2022-03-08T12:18:45","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=9561"},"modified":"2022-03-08T04:19:55","modified_gmt":"2022-03-08T12:19:55","slug":"leetcode-2197-replace-non-coprime-numbers-in-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/stack\/leetcode-2197-replace-non-coprime-numbers-in-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2197. Replace Non-Coprime Numbers in Array"},"content":{"rendered":"\n<p>You are given an array of integers&nbsp;<code>nums<\/code>. Perform the following steps:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Find&nbsp;<strong>any<\/strong>&nbsp;two&nbsp;<strong>adjacent<\/strong>&nbsp;numbers in&nbsp;<code>nums<\/code>&nbsp;that are&nbsp;<strong>non-coprime<\/strong>.<\/li><li>If no such numbers are found,&nbsp;<strong>stop<\/strong>&nbsp;the process.<\/li><li>Otherwise, delete the two numbers and&nbsp;<strong>replace<\/strong>&nbsp;them with their&nbsp;<strong>LCM (Least Common Multiple)<\/strong>.<\/li><li><strong>Repeat<\/strong>&nbsp;this process as long as you keep finding two adjacent non-coprime numbers.<\/li><\/ol>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>final<\/strong>&nbsp;modified array.<\/em>&nbsp;It can be shown that replacing adjacent non-coprime numbers in&nbsp;<strong>any<\/strong>&nbsp;arbitrary order will lead to the same result.<\/p>\n\n\n\n<p>The test cases are generated such that the values in the final array are&nbsp;<strong>less than or equal<\/strong>&nbsp;to&nbsp;<code>10<sup>8<\/sup><\/code>.<\/p>\n\n\n\n<p>Two values&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;are&nbsp;<strong>non-coprime<\/strong>&nbsp;if&nbsp;<code>GCD(x, y) &gt; 1<\/code>&nbsp;where&nbsp;<code>GCD(x, y)<\/code>&nbsp;is the&nbsp;<strong>Greatest Common Divisor<\/strong>&nbsp;of&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/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 = [6,4,3,2,7,6,2]\n<strong>Output:<\/strong> [12,7,6]\n<strong>Explanation:<\/strong> \n- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12<\/u><\/strong>,3,2,7,6,2].\n- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12<\/u><\/strong>,2,7,6,2].\n- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12<\/u><\/strong>,7,6,2].\n- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,<strong>6<\/strong>].\nThere are no more adjacent non-coprime numbers in nums.\nThus, the final modified array is [12,7,6].\nNote that there are other ways to obtain the same resultant array.\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 = [2,2,1,1,3,3,3]\n<strong>Output:<\/strong> [2,1,1,3]\n<strong>Explanation:<\/strong> \n- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<strong>3<\/strong>,3].\n- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<strong>3<\/strong>].\n- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<strong>2<\/strong>,1,1,3].\nThere are no more adjacent non-coprime numbers in nums.\nThus, the final modified array is [2,1,1,3].\nNote that there are other ways to obtain the same resultant array.\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<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li>The test cases are generated such that the values in the final array are&nbsp;<strong>less than or equal<\/strong>&nbsp;to&nbsp;<code>10<sup>8<\/sup><\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Stack<\/strong><\/h2>\n\n\n\n<p>&#8220;&#8221;&#8221;It can be shown that replacing adjacent non-coprime numbers in&nbsp;<strong>any<\/strong>&nbsp;arbitrary order will lead to the same result.&#8221;&#8221;&#8221;<\/p>\n\n\n\n<p>So that we can do it in one pass from left to right using a stack\/vector.<\/p>\n\n\n\n<p>Push the current number onto stack, and merge top two if they are not co-prime.<\/p>\n\n\n\n<p>Time complexity: O(nlogm)<br>Space complexity: O(n)<\/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<int> replaceNonCoprimes(vector<int>& nums) {\n    vector<int> ans;\n    for (int x : nums) {\n      ans.push_back(x);\n      while (ans.size() > 1) {\n        const int n1 = ans[ans.size() - 1]; \n        const int n2 = ans[ans.size() - 2]; \n        const int d = gcd(n1, n2);\n        if (d == 1) break;\n        ans.pop_back();\n        ans.pop_back();\n        ans.push_back(n1 \/ d * n2);\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an array of integers&nbsp;nums. Perform the following steps: Find&nbsp;any&nbsp;two&nbsp;adjacent&nbsp;numbers in&nbsp;nums&nbsp;that are&nbsp;non-coprime. If no such numbers are found,&nbsp;stop&nbsp;the process. Otherwise, delete the two&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[407],"tags":[771,359,217,360,180],"class_list":["post-9561","post","type-post","status-publish","format-standard","hentry","category-stack","tag-co-prime","tag-gcd","tag-hard","tag-lcm","tag-stack","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9561","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=9561"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9561\/revisions"}],"predecessor-version":[{"id":9564,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/9561\/revisions\/9564"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=9561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=9561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=9561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}