Replace Non-Coprime Numbers in Array - Problem

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.

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.

Input & Output

Example 1 — Basic Merging
$ Input: nums = [6,4,3,2,21]
Output: [84]
💡 Note: Using stack approach: 6→[6], then 4 merges with 6 to get LCM(6,4)=12→[12], then 3 merges with 12 to get LCM(12,3)=12→[12], then 2 merges with 12 to get LCM(12,2)=12→[12], finally 21 merges with 12 to get LCM(12,21)=84→[84].
Example 2 — Simple Case
$ Input: nums = [2,3,4]
Output: [2,3,4]
💡 Note: GCD(2,3)=1 and GCD(3,4)=1, all pairs are coprime, so no merging occurs. Return original array.
Example 3 — Complete Merge
$ Input: nums = [4,6,15,35]
Output: [420]
💡 Note: GCD(6,15)=3>1, merge to get [4,30,35]. Then GCD(4,30)=2>1, merge to get [60,35]. Finally GCD(60,35)=5>1, merge to get [420].

Constraints

  • 1 ≤ nums.length ≤ 105
  • 1 ≤ nums[i] ≤ 106
  • The final array values are ≤ 108

Visualization

Tap to expand
Replace Non-Coprime Numbers in Array INPUT nums = [6, 4, 3, 2, 21] 6 4 3 2 21 [0] [1] [2] [3] [4] Non-Coprime Definition: Two numbers x, y where GCD(x, y) > 1 Adjacent Pairs Check: GCD(6,4)=2 > 1 [OK] GCD(4,3)=1 [NO] GCD(3,2)=1 [NO] GCD(2,21)=1 [NO] GCD(3,21)=3 > 1 [OK] Use Stack for efficiency Process right to left ALGORITHM STEPS 1 Initialize Stack Use stack to track elements 2 Process Each Number Check GCD with stack top 3 Replace with LCM LCM = (a*b)/GCD(a,b) 4 Repeat Until Done Keep merging non-coprimes PROCESSING TRACE: Stack: [6] --> push 4 GCD(6,4)=2, LCM=12 Stack: [12] --> push 3 GCD(12,3)=3, LCM=12 Stack: [12] --> push 2 GCD(12,2)=2, LCM=12 Stack: [12] --> push 21 GCD(12,21)=3, LCM=84 GCD(6,84)=6, LCM=252 FINAL RESULT Output Array: 6 252 [6, 252] Why 252? 4,3,2,21 all merged into 252: LCM(4,6) = 12 LCM(12,3) = 12 LCM(12,2) = 12 LCM(12,21) = 84, LCM(6,84) = 252 6 stays: GCD(6,252)=6 > 1 merged into final LCM! Complexity: O(n * log(max)) Space: O(n) for stack Key Insight: Use a stack to simulate the merging process. For each new element, keep checking if it's non-coprime with the stack top. If GCD > 1, pop and replace with LCM, then continue checking. This handles cascading merges efficiently. The stack ensures we process all adjacent non-coprime pairs in a single pass through the array. TutorialsPoint - Replace Non-Coprime Numbers in Array | Optimal Stack-Based Solution
Asked in
Google 15 Meta 8 Microsoft 6
28.0K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen