Problem Description
Given two integer arrays, nums and divisors, compute the divisibility score for each element in divisors, where the score is defined as the count of numbers in nums that are divisible by that divisor. Return the divisor with the maximum score. In the event of a tie, return the smallest divisor.
Key Insights
- Iterate over each divisor and count how many numbers in nums are divisible by it.
- Since both arrays are at most 1000 in length, a simple double loop (brute force) is efficient.
- Maintain a record of the maximum score and update the candidate divisor accordingly.
- Ensure to handle ties by choosing the smallest divisor when scores are equal.
Space and Time Complexity
Time Complexity: O(n * m), where n is the length of divisors and m is the length of nums.
Space Complexity: O(1), as only a few additional variables are used.
Solution
The solution involves iterating through each divisor in the divisors array and, for each one, iterating through the nums array to count how many elements are divisible by it. Keep track of the highest count (divisibility score) and update the answer if a higher score is found. In case of a tie, select the smaller divisor. This direct approach leverages simple loops and conditional comparisons, ensuring clarity and correctness within the given constraints.