LeetCode 2644: Find the Maximum Divisibility Score Solution

Master LeetCode problem 2644 (Find the Maximum Divisibility Score), a easy 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.

2644. Find the Maximum Divisibility Score

Easy

Problem Explanation

Explanation

To solve this problem, we can iterate over each divisor and count the number of elements in the nums array that are divisible by that divisor. We then track the divisor with the maximum count. If there are multiple divisors with the same maximum count, we return the smallest one.

  • Initialize a map to store the count of elements divisible by each divisor.
  • Iterate over the nums array and for each element, iterate over the divisors array to check if the element is divisible by the divisor. Increment the count in the map accordingly.
  • Finally, find the divisor with the maximum count. If there are multiple divisors with the same maximum count, return the smallest one.

Time Complexity: O(N * M), where N is the length of the nums array and M is the length of the divisors array.

Space Complexity: O(M), where M is the length of the divisors array.

Solution Code

class Solution {
    public int maxDivisor(int[] nums, int[] divisors) {
        Map<Integer, Integer> countMap = new HashMap<>();
        
        for (int num : nums) {
            for (int divisor : divisors) {
                if (num % divisor == 0) {
                    countMap.put(divisor, countMap.getOrDefault(divisor, 0) + 1);
                }
            }
        }
        
        int maxDivisor = Integer.MAX_VALUE;
        int maxCount = 0;
        
        for (int divisor : divisors) {
            if (countMap.containsKey(divisor) && countMap.get(divisor) > maxCount) {
                maxDivisor = divisor;
                maxCount = countMap.get(divisor);
            } else if (countMap.containsKey(divisor) && countMap.get(divisor) == maxCount) {
                maxDivisor = Math.min(maxDivisor, divisor);
            }
        }
        
        return maxDivisor;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2644 (Find the Maximum Divisibility Score)?

This page provides optimized solutions for LeetCode problem 2644 (Find the Maximum Divisibility Score) 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 2644 (Find the Maximum Divisibility Score)?

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

Can I run code for LeetCode 2644 on DevExCode?

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

Back to LeetCode Solutions