Find the Maximum Divisibility Score — LeetCode 2644 Python Solution

EasyArray
Problem
#2644
Reading time
2 min

The problem

You are given two integer arrays nums and divisors. The divisibility score of divisors[i] is the number of indices j such that nums[j] is divisible by divisors[i].

Python solution

Python
class Solution:
    def maxDivScore(self, nums: List[int], divisors: List[int]) -> int:
        ans, mx = divisors[0], 0
        for div in divisors:
            cnt = sum(x % div == 0 for x in nums)
            if mx < cnt:
                mx, ans = cnt, div
            elif mx == cnt and ans > div:
                ans = div
        return ans

Complexity

MeasureComplexity
TimeO(m \times n), where m and n are the lengths of nums and divisors respectively
SpaceO(1) auxiliary

Related problems

Frequently asked questions

How hard is LeetCode 2644. Find the Maximum Divisibility Score?
LeetCode 2644. Find the Maximum Divisibility Score is rated Easy on LeetCode.
What is the time complexity of LeetCode 2644. Find the Maximum Divisibility Score?
The Python solution on this page runs in O(m \times n), where m and n are the lengths of nums and divisors respectively.
What is the space complexity of LeetCode 2644. Find the Maximum Divisibility Score?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2644. Find the Maximum Divisibility Score cover?
LeetCode 2644. Find the Maximum Divisibility Score is tagged Array on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview