Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times:
i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value.Return the minimum number of operations to make all elements of nums equal to 1. If it is impossible, return -1.
The gcd of two integers is the greatest common divisor of the two integers.
Example 1:
Input: nums = [2,6,3,4] Output: 4 Explanation: We can do the following operations: - Choose index i = 2 and replace nums[2] with gcd(3,4) = 1. Now we have nums = [2,6,1,4]. - Choose index i = 1 and replace nums[1] with gcd(6,1) = 1. Now we have nums = [2,1,1,4]. - Choose index i = 0 and replace nums[0] with gcd(2,1) = 1. Now we have nums = [1,1,1,4]. - Choose index i = 2 and replace nums[3] with gcd(1,4) = 1. Now we have nums = [1,1,1,1].
Example 2:
Input: nums = [2,10,6,14] Output: -1 Explanation: It can be shown that it is impossible to make all the elements equal to 1.
Constraints:
2 <= nums.length <= 501 <= nums[i] <= 106Problem summary: You are given a 0-indexed array nums consisting of positive integers. You can do the following operation on the array any number of times: Select an index i such that 0 <= i < n - 1 and replace either of nums[i] or nums[i+1] with their gcd value. Return the minimum number of operations to make all elements of nums equal to 1. If it is impossible, return -1. The gcd of two integers is the greatest common divisor of the two integers.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[2,6,3,4]
[2,10,6,14]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2654: Minimum Number of Operations to Make All Array Elements Equal to 1
class Solution {
public int minOperations(int[] nums) {
int n = nums.length;
int cnt = 0;
for (int x : nums) {
if (x == 1) {
++cnt;
}
}
if (cnt > 0) {
return n - cnt;
}
int mi = n + 1;
for (int i = 0; i < n; ++i) {
int g = 0;
for (int j = i; j < n; ++j) {
g = gcd(g, nums[j]);
if (g == 1) {
mi = Math.min(mi, j - i + 1);
}
}
}
return mi > n ? -1 : n - 1 + mi - 1;
}
private int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
// Accepted solution for LeetCode #2654: Minimum Number of Operations to Make All Array Elements Equal to 1
func minOperations(nums []int) int {
n := len(nums)
cnt := 0
for _, x := range nums {
if x == 1 {
cnt++
}
}
if cnt > 0 {
return n - cnt
}
mi := n + 1
for i := 0; i < n; i++ {
g := 0
for j := i; j < n; j++ {
g = gcd(g, nums[j])
if g == 1 {
mi = min(mi, j-i+1)
}
}
}
if mi > n {
return -1
}
return n - 1 + mi - 1
}
func gcd(a, b int) int {
if b == 0 {
return a
}
return gcd(b, a%b)
}
# Accepted solution for LeetCode #2654: Minimum Number of Operations to Make All Array Elements Equal to 1
class Solution:
def minOperations(self, nums: List[int]) -> int:
n = len(nums)
cnt = nums.count(1)
if cnt:
return n - cnt
mi = n + 1
for i in range(n):
g = 0
for j in range(i, n):
g = gcd(g, nums[j])
if g == 1:
mi = min(mi, j - i + 1)
return -1 if mi > n else n - 1 + mi - 1
// Accepted solution for LeetCode #2654: Minimum Number of Operations to Make All Array Elements Equal to 1
impl Solution {
pub fn min_operations(nums: Vec<i32>) -> i32 {
let n = nums.len() as i32;
let cnt = nums.iter().filter(|&&x| x == 1).count() as i32;
if cnt > 0 {
return n - cnt;
}
let mut mi = n + 1;
for i in 0..nums.len() {
let mut g = 0;
for j in i..nums.len() {
g = gcd(g, nums[j]);
if g == 1 {
mi = mi.min((j - i + 1) as i32);
break;
}
}
}
if mi > n {
-1
} else {
n - 1 + mi - 1
}
}
}
fn gcd(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let tmp = a % b;
a = b;
b = tmp;
}
a.abs()
}
// Accepted solution for LeetCode #2654: Minimum Number of Operations to Make All Array Elements Equal to 1
function minOperations(nums: number[]): number {
const n = nums.length;
let cnt = 0;
for (const x of nums) {
if (x === 1) {
++cnt;
}
}
if (cnt > 0) {
return n - cnt;
}
let mi = n + 1;
for (let i = 0; i < n; ++i) {
let g = 0;
for (let j = i; j < n; ++j) {
g = gcd(g, nums[j]);
if (g === 1) {
mi = Math.min(mi, j - i + 1);
}
}
}
return mi > n ? -1 : n - 1 + mi - 1;
}
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % b);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.