Skip to content

Practice Minimum Number of Operations to Make All Array Elements Equal to 1

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2654
LeetCode Problem

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

Problem Statement

In one operation, choose two adjacent nums entries, compute their greatest common divisor, and replace either one of those entries with it. Repeat as needed. Return the minimum operations to make every entry 1, or -1 if impossible.

Source: Minimum Number of Operations to Make All Array Elements Equal to 1 on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [2,6,3,4]
Output
4

# Example 2

Input
nums = [2,10,6,14]
Output
-1

Constraints

2 <= nums.length <= 50
1 <= nums[i] <= 10^6

Related LeetCode Problems

Practice more problems similar to Minimum Number of Operations to Make All Array Elements Equal to 1.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.