Search
⌘K

Leetcode 2654. Minimum Number of Operations to Make All Array Elements Equal to 1

You may replace either number of any adjacent pair with their gcd; compute the minimum operations to make every element 1 (or return -1 if impossible). Key idea: if any element is 1 the answer is n - count(1); if gcd(all) > 1 it's impossible; otherwise find the shortest subarray whose gcd is 1 and the minimum operations = n + (subarray length) - 2.


Question Timeline

See when this question was last asked and where, including any notes left by other candidates.

Your account is free and you can post anonymously if you choose.