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].
Return the integer divisors[i] with the maximum divisibility score. If multiple integers have the maximum score, return the smallest one.
Example 1:
Input: nums = [2,9,15,50], divisors = [5,3,7,2]
Output: 2
Explanation:
The divisibility score of divisors[0] is 2 since nums[2] and nums[3] are divisible by 5.
The divisibility score of divisors[1] is 2 since nums[1] and nums[2] are divisible by 3.
The divisibility score of divisors[2] is 0 since none of the numbers in nums is divisible by 7.
The divisibility score of divisors[3] is 2 since nums[0] and nums[3] are divisible by 2.
As divisors[0], divisors[1], and divisors[3] have the same divisibility score, we return the smaller one which is divisors[3].
Example 2:
Input: nums = [4,7,9,3,9], divisors = [5,2,3]
Output: 3
Explanation:
The divisibility score of divisors[0] is 0 since none of numbers in nums is divisible by 5.
The divisibility score of divisors[1] is 1 since only nums[0] is divisible by 2.
The divisibility score of divisors[2] is 3 since nums[2], nums[3] and nums[4] are divisible by 3.
Example 3:
Input: nums = [20,14,21,10], divisors = [10,16,20]
Output: 10
Explanation:
The divisibility score of divisors[0] is 2 since nums[0] and nums[3] are divisible by 10.
The divisibility score of divisors[1] is 0 since none of the numbers in nums is divisible by 16.
The divisibility score of divisors[2] is 1 since nums[0] is divisible by 20.
Constraints:
1 <= nums.length, divisors.length <= 10001 <= nums[i], divisors[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding the maximum divisibility score means we try every possible number from the divisors and count how many numbers in the given number list it divides. We then pick the divisor with the highest count.
Here's how the algorithm would work step-by-step:
def find_max_divisibility_score_brute_force(numbers, divisors):
maximum_score = -1
best_divisor = -1
for current_divisor in divisors:
current_score = 0
# Iterate through each number to calculate score
for number_to_check in numbers:
if number_to_check % current_divisor == 0:
current_score += 1
# If current score is greater, update the best divisor
if current_score > maximum_score:
maximum_score = current_score
best_divisor = current_divisor
# If scores are equal, choose the smallest divisor
elif current_score == maximum_score:
#Ensure we select the SMALLEST value
best_divisor = min(best_divisor, current_divisor)
return best_divisorTo efficiently find the maximum divisibility score, we count how many numbers in the input list are divisible by each number in the divisors list. Then, we identify the divisor with the highest count (the maximum divisibility score). If there's a tie, we choose the smallest divisor.
Here's how the algorithm would work step-by-step:
def find_max_divisibility_score(numbers, divisors):
maximum_score = -1
best_divisor = -1
for current_divisor in divisors:
divisibility_score = 0
# Calculate the score for the current divisor.
for number in numbers:
if number % current_divisor == 0:
divisibility_score += 1
# If a better score is found, update the best divisor.
if divisibility_score > maximum_score:
maximum_score = divisibility_score
best_divisor = current_divisor
# Tiebreaker: Choose the smaller divisor.
elif divisibility_score == maximum_score:
if current_divisor < best_divisor:
best_divisor = current_divisor
return best_divisor| Case | How to Handle |
|---|---|
| nums is null or empty | Return -1 or throw an exception as there are no numbers to check for divisibility. |
| divisors is null or empty | Return -1 as there are no divisors to check against the nums array. |
| nums and divisors are both empty | Return -1 as there are no divisors or numbers to compare. |
| nums contains zero values | Divisors should not be zero, but zero can be divisible by non-zero values, which needs to be accounted in count. |
| divisors contains zero | Ignore divisors equal to zero since division by zero is undefined. |
| nums contains large integer values potentially leading to overflow | The division operation itself shouldn't cause an overflow as long as divisors are also integers, so no special handling is needed, assuming integer division is used. |
| All numbers in nums are the same and divisible by all numbers in divisors | The solution should iterate through the divisors and return the smallest divisor with the maximum divisibility score. |
| Large input arrays for nums and divisors (performance considerations) | Ensure the solution iterates efficiently through the arrays to avoid exceeding time limits, potentially using optimized loops or early termination strategies. |