Minimum Number of Operations to Make All Array Elements Equal to 1 - Problem

Imagine you're a mathematician tasked with simplifying an array of positive integers until every element becomes 1. You have a powerful tool at your disposal: the ability to replace any element with the greatest common divisor (GCD) of itself and its immediate neighbor.

The Challenge: Given an array nums of positive integers, you can perform the following operation any number of times:

  • Select an index i where 0 โ‰ค i < n-1
  • Replace either nums[i] or nums[i+1] with gcd(nums[i], nums[i+1])

Your goal is to find the minimum number of operations needed to make all array elements equal to 1. If it's impossible to achieve this goal, return -1.

Key Insight: The GCD operation can only make numbers smaller or keep them the same, so we need to strategically choose which elements to replace to propagate 1s throughout the array efficiently.

Input & Output

example_1.py โ€” Basic case with existing 1
$ Input: [2,6,3,4]
โ€บ Output: 4
๐Ÿ’ก Note: We can make gcd(3,4) = 1, then replace one element with 1. After that, we need 3 more operations to make all other elements equal to 1: gcd(2,1)=1, gcd(6,1)=1, and we already have two 1s.
example_2.py โ€” Array with existing 1s
$ Input: [2,10,6,14]
โ€บ Output: 4
๐Ÿ’ก Note: Since gcd(2,10,6,14) = 2 > 1, we cannot make all elements equal to 1. However, we can create pairs that eventually lead to 1: gcd(10,6)=2, then gcd(2,2)=2, then we need to find another path. Actually, gcd(6,14)=2, gcd(2,10)=2, then gcd(2,2)=2. We need to use the property that gcd(2,6)=2, gcd(2,14)=2, and eventually through multiple operations we can reach 1.
example_3.py โ€” Impossible case
$ Input: [6,10,15]
โ€บ Output: -1
๐Ÿ’ก Note: The gcd of all elements is gcd(6,10,15) = 1, which means it might be possible. However, we need to check if we can actually create a 1 through adjacent operations: gcd(6,10)=2, gcd(10,15)=5, gcd(2,15)=1. So we can make it work with operations.

Constraints

  • 1 โ‰ค nums.length โ‰ค 50
  • 1 โ‰ค nums[i] โ‰ค 106
  • Adjacent operations only: You can only perform GCD operations on adjacent elements

Visualization

Tap to expand
GCD Simplification StrategyScenario 1: Array contains 1s6149โ†’ Already has 1! Need 3 operations to spreadScenario 2: No 1s, need to create one first694Step 1: Find gcd(9,4) = 1614Step 2: Now spread the 1 (2 more operations)111Total: 1 + 2 = 3 operationsScenario 3: Impossible case61014All even numbers, gcd > 1 always, return -1๐Ÿ’ก Key: Once you have a 1, spreading is always n-1 operations!
Understanding the Visualization
1
Identify Existing 1s
Count elements that are already simplified (equal to 1)
2
Calculate Spread Cost
If 1s exist, cost = n - count_of_ones operations to spread them
3
Find Creation Path
If no 1s exist, find shortest subarray that can create GCD = 1
4
Optimize Total Cost
Minimum cost = creation_cost + spreading_cost
Key Takeaway
๐ŸŽฏ Key Insight: The problem transforms from complex state exploration to simple counting: either spread existing 1s (n - count_of_ones operations) or find the cheapest way to create a 1 first, then spread it. The mathematical properties of GCD make this optimization possible.
Asked in
Google 28 Meta 15 Amazon 12 Microsoft 8
23.5K Views
Medium Frequency
~15 min Avg. Time
847 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