Replace Non-Coprime Numbers in Array - Problem

๐Ÿ”ข Replace Non-Coprime Numbers in Array

Imagine you have an array of integers, and you need to perform a merging process that combines adjacent numbers that share common factors!

Your Task: Given an array of integers nums, repeatedly find any two adjacent numbers that are non-coprime (meaning they share a common factor greater than 1), and replace them with their LCM (Least Common Multiple).

๐ŸŽฏ The Process:

  1. Find any two adjacent numbers in nums that are non-coprime
  2. If no such pair exists, stop the process
  3. Otherwise, delete both numbers and replace them with their LCM
  4. Repeat until no more adjacent non-coprime pairs exist

Key Concept: Two numbers x and y are non-coprime if GCD(x, y) > 1, meaning they share a common factor other than 1.

Example: [6, 4, 3, 2, 6]
โ€ข 6 and 4 are non-coprime (GCD = 2), replace with LCM = 12
โ€ข Result: [12, 3, 2, 6]
โ€ข Continue until no more merges possible...

๐ŸŽ‰ Fascinating Property: The order of processing doesn't matter - you'll always get the same final result!

Input & Output

example_1.py โ€” Basic Merging
$ Input: nums = [6, 4, 3, 2, 6]
โ€บ Output: [12, 12]
๐Ÿ’ก Note: Step 1: GCD(6,4) = 2 > 1, so merge to LCM(6,4) = 12, array becomes [12, 3, 2, 6]. Step 2: GCD(12,3) = 3 > 1, merge to LCM(12,3) = 12, array becomes [12, 2, 6]. Step 3: GCD(2,6) = 2 > 1, merge to LCM(2,6) = 6, array becomes [12, 6]. Step 4: GCD(12,6) = 6 > 1, merge to LCM(12,6) = 12, final array is [12]. Wait, let me recalculate: After [12, 2, 6], no adjacent pairs are non-coprime initially, but we need to check systematically.
example_2.py โ€” No Merges Needed
$ Input: nums = [2, 3, 5]
โ€บ Output: [2, 3, 5]
๐Ÿ’ก Note: All adjacent pairs are coprime: GCD(2,3) = 1, GCD(3,5) = 1. Since no pairs share common factors greater than 1, no merging occurs and the array remains unchanged.
example_3.py โ€” Complete Collapse
$ Input: nums = [4, 6, 12, 18]
โ€บ Output: [36]
๐Ÿ’ก Note: Step 1: GCD(4,6) = 2 > 1, merge to LCM(4,6) = 12, array becomes [12, 12, 18]. Step 2: GCD(12,12) = 12 > 1, merge to LCM(12,12) = 12, array becomes [12, 18]. Step 3: GCD(12,18) = 6 > 1, merge to LCM(12,18) = 36, final array is [36].

Visualization

Tap to expand
Replace Non-Coprime Numbers - Stack VisualizationInput Array ProcessingOriginal: [6, 4, 3, 2, 6]Stack maintains result while checking backward mergesFinal: [12, 12]Input Stream6 โ†’ 4 โ†’ 3 โ†’ 2 โ†’ 6Stack Operations64โ†’12Merge: LCM(6,4)=12Result Stack1212Key Algorithm Steps:1Add element to stack2Check if top 2 elements have GCD > 13If non-coprime, replace with LCM and repeat4Continue until no more merges possibleTime Complexity Analysisโ€ข Each element pushed at most once: O(n)โ€ข Each element popped at most once: O(n)โ€ข GCD calculations: O(log min(a,b))โ€ข Overall: O(n) with efficient merging๐ŸŽฏ Stack-based approach ensures optimal O(n) processing!
Understanding the Visualization
1
Initialize Stack
Start with empty result stack to build our final array
2
Add Element
Add current input element to top of stack
3
Check Merge
Check if top two stack elements are non-coprime (GCD > 1)
4
Merge if Possible
Replace non-coprime pair with their LCM and repeat check
5
Continue Process
Move to next input element and repeat until array complete
Key Takeaway
๐ŸŽฏ Key Insight: Stack-based processing with backward merging ensures we handle all possible combinations efficiently in O(n) time while maintaining the correct order of operations.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Each element is pushed and popped at most once, GCD operations are O(log min(a,b)) but bounded

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Stack can hold up to n elements in worst case when no merges occur

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 106
  • Values in final array โ‰ค 108
  • Two numbers are non-coprime if their GCD > 1
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
28.5K Views
Medium Frequency
~25 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