LeetCode 2654: Minimum Number of Operations to Make All Array Elements Equal to 1 Solution

Master LeetCode problem 2654 (Minimum Number of Operations to Make All Array Elements Equal to 1), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

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

Problem Explanation

Explanation

To solve this problem, we can iterate through the array and try to make all elements equal to 1. We will keep track of the operations needed to achieve this. We will use the property that the GCD of a number and 1 is always 1. We will start by finding the GCD of adjacent elements and replacing one of them with the GCD value until all elements are equal to 1. If at any point we encounter a number that is not divisible by 1, we know it is impossible to make all elements equal to 1.

Algorithm:

  1. Iterate through the array and find the GCD of each pair of adjacent elements.
  2. Replace one of the elements with the GCD value and increment the operations count.
  3. Continue this process until all elements are equal to 1 or it is impossible.
  4. Return the minimum number of operations required to make all elements equal to 1.

Time Complexity: O(nlog(max(nums))) Space Complexity: O(1)

Solution Code

class Solution {
    public int minOperations(int[] nums) {
        int n = nums.length;
        int operations = 0;
        
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] % 2 != 0) {
                return -1; // If any number is not divisible by 2, it is impossible
            }
            
            int gcd = gcd(nums[i], nums[i + 1]);
            nums[i + 1] = gcd;
            operations++;
        }
        
        return operations;
    }
    
    private int gcd(int a, int b) {
        if (b == 0) {
            return a;
        }
        return gcd(b, a % b);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2654 (Minimum Number of Operations to Make All Array Elements Equal to 1)?

This page provides optimized solutions for LeetCode problem 2654 (Minimum Number of Operations to Make All Array Elements Equal to 1) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2654 (Minimum Number of Operations to Make All Array Elements Equal to 1)?

The time complexity for LeetCode 2654 (Minimum Number of Operations to Make All Array Elements Equal to 1) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2654 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2654 in Java, C++, or Python.

Back to LeetCode Solutions