java.lang.Object
g2101_2200.s2197_replace_non_coprime_numbers_in_array.Solution

public class Solution extends Object
2197 - Replace Non-Coprime Numbers in Array.<p>Hard</p> <p>You are given an array of integers <code>nums</code>. Perform the following steps:</p> <ol> <li>Find <strong>any</strong> two <strong>adjacent</strong> numbers in <code>nums</code> that are <strong>non-coprime</strong>.</li> <li>If no such numbers are found, <strong>stop</strong> the process.</li> <li>Otherwise, delete the two numbers and <strong>replace</strong> them with their <strong>LCM (Least Common Multiple)</strong>.</li> <li><strong>Repeat</strong> this process as long as you keep finding two adjacent non-coprime numbers.</li> </ol> <p>Return <em>the <strong>final</strong> modified array.</em> It can be shown that replacing adjacent non-coprime numbers in <strong>any</strong> arbitrary order will lead to the same result.</p> <p>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>10<sup>8</sup></code>.</p> <p>Two values <code>x</code> and <code>y</code> are <strong>non-coprime</strong> if <code>GCD(x, y) > 1</code> where <code>GCD(x, y)</code> is the <strong>Greatest Common Divisor</strong> of <code>x</code> and <code>y</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [6,4,3,2,7,6,2]</p> <p><strong>Output:</strong> [12,7,6]</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>(6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong>12</strong> ,3,2,7,6,2].</p> </li> <li> <p>(12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong>12</strong> ,2,7,6,2].</p> </li> <li> <p>(12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong>12</strong> ,7,6,2].</p> </li> <li> <p>(6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7, <strong>6</strong> ].</p> </li> </ul> <p>There are no more adjacent non-coprime numbers in nums.</p> <p>Thus, the final modified array is [12,7,6].</p> <p>Note that there are other ways to obtain the same resultant array.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [2,2,1,1,3,3,3]</p> <p><strong>Output:</strong> [2,1,1,3]</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>(3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1, <strong>3</strong> ,3].</p> </li> <li> <p>(3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1, <strong>3</strong> ].</p> </li> <li> <p>(2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<strong>2</strong> ,1,1,3].</p> </li> </ul> <p>There are no more adjacent non-coprime numbers in nums.</p> <p>Thus, the final modified array is [2,1,1,3].</p> <p>Note that there are other ways to obtain the same resultant array.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> <li><code>1 <= nums[i] <= 10<sup>5</sup></code></li> <li>The test cases are generated such that the values in the final array are <strong>less than or equal</strong> to <code>10<sup>8</sup></code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • replaceNonCoprimes

      public List<Integer> replaceNonCoprimes(int[] nums)