Find the Maximum Divisibility Score
You want to find the divisor with the maximum divisibility score from the array divisors
. If there are multiple divisors with the same maximum score, you want to return the smallest one.
Here’s a step-by-step guide to solve this problem:
- Initialize variables to keep track of the maximum divisibility score and the corresponding divisor.
- Iterate through the
divisors
array, and for each divisor, calculate its divisibility score by checking how many numbers in thenums
array are divisible by it. - If the current divisor’s divisibility score is higher than the current maximum score, update the maximum score and the corresponding divisor. If the score is equal to the current maximum score, update the divisor only if the current divisor is smaller.
- Return the divisor that corresponds to the maximum divisibility score.
Here is the code:
|
|
Explanation:
score = sum(1 for num in nums if num % divisor == 0)
calculates the divisibility score by summing up 1 for every number innums
that is divisible by the current divisor.if score > max_score or (score == max_score and divisor < best_divisor):
checks whether the current divisor has a higher divisibility score or has the same score but is smaller than the current best divisor. If so, the maximum score and the best divisor are updated.- Finally, the code returns the divisor with the maximum divisibility score.