Find the Maximum Divisibility Score - Problem

You are given two integer arrays: nums and divisors. Your task is to find which divisor has the highest divisibility score.

The divisibility score of a divisor is simply the count of how many numbers in nums are divisible by that divisor. For example, if nums = [4, 7, 9, 3, 9] and we're checking divisor 3, we count how many numbers (4, 7, 9, 3, 9) are divisible by 3. In this case, both 9s and 3 are divisible by 3, giving us a score of 3.

Goal: Return the divisor with the maximum divisibility score. If there's a tie (multiple divisors have the same highest score), return the smallest divisor among them.

Example: nums = [4, 7, 9, 3, 9], divisors = [5, 2, 3]
• Divisor 5: score = 0 (no numbers divisible by 5)
• Divisor 2: score = 1 (only 4 is divisible by 2)
• Divisor 3: score = 3 (numbers 9, 3, 9 are divisible by 3)
Answer: 3 (highest score)

Input & Output

example_1.py — Basic Case
$ Input: nums = [4, 7, 9, 3, 9], divisors = [5, 2, 3]
Output: 3
💡 Note: Divisor 5: 0 numbers divisible (score=0). Divisor 2: 1 number divisible [4] (score=1). Divisor 3: 3 numbers divisible [9, 3, 9] (score=3). Maximum score is 3.
example_2.py — Tie Breaker
$ Input: nums = [20, 14, 21, 10], divisors = [10, 7, 5]
Output: 5
💡 Note: Divisor 10: 2 numbers divisible [20, 10] (score=2). Divisor 7: 2 numbers divisible [14, 21] (score=2). Divisor 5: 2 numbers divisible [20, 10] (score=2). All have same score, return smallest divisor which is 5.
example_3.py — No Matches
$ Input: nums = [3], divisors = [2]
Output: 2
💡 Note: Only one divisor 2, and 3 is not divisible by 2, so score is 0. Still return the only available divisor which is 2.

Constraints

  • 1 ≤ nums.length, divisors.length ≤ 1000
  • 1 ≤ nums[i], divisors[i] ≤ 109
  • All divisors are positive integers
  • Return the smallest divisor in case of tie

Visualization

Tap to expand
🎯 Find the Best Grade ThresholdStudent Scores:47939Threshold: 5Students passing: 0Too high - no one passesThreshold: 2Students passing: 1Student with score 4 passesThreshold: 3Students passing: 3Scores 9, 3, 9 pass ✓📊 Analysis ResultsBest Threshold: 3Allows maximum students (3) to pass
Understanding the Visualization
1
Set Up Thresholds
Teacher has multiple grade thresholds to test: 5, 2, and 3
2
Test First Threshold
Check threshold 5: No students meet this high standard (score=0)
3
Test Second Threshold
Check threshold 2: One student meets this standard (score=1)
4
Test Final Threshold
Check threshold 3: Three students meet this standard (score=3) - Best choice!
Key Takeaway
🎯 Key Insight: Just like a teacher finding the optimal grade threshold, we systematically test each divisor to find the one that 'passes' the most numbers, always preferring smaller divisors when there's a tie.
Asked in
Amazon 12 Google 8 Microsoft 6 Meta 4
28.5K Views
Medium Frequency
~15 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen