LeetCode 3334: Find the Maximum Factor Score of Array Solution

Master LeetCode problem 3334 (Find the Maximum Factor Score of Array), 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.

3334. Find the Maximum Factor Score of Array

Problem Explanation

Explanation

To find the maximum factor score of the array after removing at most one element, we need to consider the LCM and GCD of the array elements with and without removing each element. We can achieve this by iterating through the array, calculating the GCD and LCM of the elements, and comparing the factor score when each element is removed. The maximum factor score will be the highest factor score obtained.

  • Initialize variables to keep track of the maximum factor score.
  • Iterate through the array and for each element:
    • Calculate GCD and LCM of all elements excluding the current element.
    • Update the maximum factor score if the product of GCD and LCM is greater than the current maximum.
  • Return the maximum factor score.

Time complexity: O(n^2) where n is the number of elements in the array.
Space complexity: O(1)

Solution Code

class Solution {
    public int maxFactorScore(int[] nums) {
        int maxScore = 0;
        
        for (int i = 0; i < nums.length; i++) {
            int currScore = 0;
            int gcd = 0;
            int lcm = 1;
            
            for (int j = 0; j < nums.length; j++) {
                if (j != i) {
                    gcd = calculateGCD(gcd, nums[j]);
                    lcm = calculateLCM(lcm, nums[j]);
                }
            }
            
            currScore = gcd * lcm;
            maxScore = Math.max(maxScore, currScore);
        }
        
        return maxScore;
    }
    
    private int calculateGCD(int a, int b) {
        if (b == 0) {
            return a;
        }
        return calculateGCD(b, a % b);
    }
    
    private int calculateLCM(int a, int b) {
        return a * b / calculateGCD(a, b);
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 3334 (Find the Maximum Factor Score of Array)?

This page provides optimized solutions for LeetCode problem 3334 (Find the Maximum Factor Score of Array) 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 3334 (Find the Maximum Factor Score of Array)?

The time complexity for LeetCode 3334 (Find the Maximum Factor Score of Array) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 3334 on DevExCode?

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

Back to LeetCode Solutions