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


	Find any two adjacent numbers in nums that are non-coprime.
	If no such numbers are found, stop the process.
	Otherwise, delete the two numbers and replace them with their LCM (Least Common Multiple).
	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) &gt; 1 where GCD(x, y) is the Greatest Common Divisor of x and y.

&nbsp;
Example 1:

Input: nums = [6,4,3,2,7,6,2]
Output: [12,7,6]
Explanation: 
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [12,3,2,7,6,2].
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [12,2,7,6,2].
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [12,7,6,2].
- (6, 2) are non-coprime with LCM(6, 2) = 6. Now, nums = [12,7,6].
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: 
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3,3].
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3].
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [2,1,1,3].
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.


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 105
	The test cases are generated such that the values in the final array are less than or equal to 108.


class Solution:
    def replaceNonCoprimes(self, nums: List[int]) -> List[int]:
        res = []
        for num in nums:
            while res and gcd(res[-1], num) > 1:
                num = lcm(res[-1], num)
                res.pop()
            res.append(num)
        return res
class Solution {
	public List<Integer> replaceNonCoprimes(int[] nums) 
	{
		List<Integer> al=new ArrayList<>();
		long n1=nums[0];
		int idx=1;

		while(idx<nums.length)
		{
			if((int)gcd(n1,nums[idx])==1)
			{
				while(al.size()!=0)
				{
					int t=al.get(al.size()-1);
					if(gcd(n1,t)==1)
						break;
					else
					{
						al.remove(al.size()-1);
						n1=lcm(t,n1);
					}
				}

				al.add((int)n1);
				n1=nums[idx];
				idx++;
			}
			else
			{
				n1=lcm(n1,nums[idx]);
				idx++;
			}
		}

		while(al.size()!=0)
		{
			int t=al.get(al.size()-1);
			if(gcd(n1,t)==1)
				break;
			else
			{
				al.remove(al.size()-1);
				n1=lcm(t,n1);
			}
		} 
		al.add((int)n1);

		return al;
	}

	public long gcd(long a,long b)
	{
		if (b == 0)   
			return a;  

		return gcd(b, a % b);  
	}

	public long lcm(long a,long b)
	{
		long hcf=gcd(a,b);
		return (a*b)/hcf;
	}
}
class Solution {
public:
    vector<int> replaceNonCoprimes(vector<int>& nums) {
        vector<int> res;
        for (auto num : nums) {
            while (!res.empty() && gcd(res.back(), num) > 1) {
                num = lcm(res.back(), num);
                res.pop_back();
            }
            res.push_back(num);
        }
        return res;
    }
};
function gcd(a, b) {
    while (b > 0) {
        a %= b;
        [a, b] = [b, a];
    }
    return a;
}
function lcm(a, b) {
    return a / gcd(a, b) * b;
}

var replaceNonCoprimes = function(nums) {
    let res = new Array();
    for (let num of nums) {
        while (res.length > 0 && gcd(res.at(-1), num) > 1) {
            num = lcm(res.at(-1), num);
            res.pop();
        }
        res.push(num);
    }
    return res;
};
Replace Non-Coprime Numbers in Array