LeetCode-in-Java

2197. Replace Non-Coprime Numbers in Array

Hard

You are given an array of integers nums. Perform the following steps:

  1. Find any two adjacent numbers in nums that are non-coprime.
  2. If no such numbers are found, stop the process.
  3. Otherwise, delete the two numbers and replace them with their LCM (Least Common Multiple).
  4. Repeat this process as long as you keep finding two adjacent non-coprime numbers.

Return the final modified array. It can be shown that replacing adjacent non-coprime numbers in any arbitrary order will lead to the same result.

The test cases are generated such that the values in the final array are less than or equal to 108.

Two values x and y are non-coprime if GCD(x, y) > 1 where GCD(x, y) is the Greatest Common Divisor of x and y.

Example 1:

Input: nums = [6,4,3,2,7,6,2]

Output: [12,7,6]

Explanation:

There are no more adjacent non-coprime numbers in nums.

Thus, the final modified array is [12,7,6].

Note that there are other ways to obtain the same resultant array.

Example 2:

Input: nums = [2,2,1,1,3,3,3]

Output: [2,1,1,3]

Explanation:

There are no more adjacent non-coprime numbers in nums.

Thus, the final modified array is [2,1,1,3].

Note that there are other ways to obtain the same resultant array.

Constraints:

Solution

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("java:S2234")
public class Solution {
    public List<Integer> replaceNonCoprimes(int[] nums) {
        ArrayList<Integer> res = new ArrayList<>();
        int i = 1;
        res.add(nums[0]);
        while (i < nums.length) {
            int first = res.get(res.size() - 1);
            int second = nums[i];
            int gcd = gcd(first, second);
            if (gcd > 1) {
                long lcm = ((long) first * (long) second) / gcd;
                if (!res.isEmpty()) {
                    res.remove(res.size() - 1);
                }
                res.add((int) lcm);
                recursivelyCheck(res);
            } else {
                res.add(second);
            }
            i++;
        }
        return res;
    }

    private int gcd(int a, int b) {
        if (a > b) {
            return gcd(b, a);
        }
        if (b % a == 0) {
            return a;
        }
        return gcd(b % a, a);
    }

    private void recursivelyCheck(ArrayList<Integer> list) {
        if (list.size() < 2) {
            return;
        }
        int a = list.remove(list.size() - 1);
        int b = list.remove(list.size() - 1);
        int gcd = gcd(a, b);
        if (gcd > 1) {
            long lcm = ((long) (a) * (long) (b)) / gcd;
            list.add((int) lcm);
            recursivelyCheck(list);
        } else {
            list.add(b);
            list.add(a);
        }
    }
}