Find the Maximum Divisibility Score - 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].

Return the integer divisors[i] with the maximum divisibility score. If multiple integers have the maximum score, return the smallest one.

Input & Output

Example 1 — Basic Case
$ Input: nums = [4,7,9,3,9], divisors = [5,2,3]
Output: 3
💡 Note: Divisor 5: no numbers divisible (score 0). Divisor 2: only 4 is divisible (score 1). Divisor 3: numbers 9, 3, 9 are divisible (score 3). Maximum score is 3, so return 3.
Example 2 — Tie Case
$ Input: nums = [20,14,21,10], divisors = [5,7,5]
Output: 5
💡 Note: Divisor 5: numbers 20, 10 are divisible (score 2). Divisor 7: numbers 14, 21 are divisible (score 2). Both have score 2, but 5 < 7, so return 5.
Example 3 — Single Element
$ Input: nums = [12], divisors = [10,16]
Output: 10
💡 Note: Divisor 10: 12 is not divisible by 10 (score 0). Divisor 16: 12 is not divisible by 16 (score 0). Both have score 0, but 10 < 16, so return 10.

Constraints

  • 1 ≤ nums.length, divisors.length ≤ 1000
  • 1 ≤ nums[i], divisors[i] ≤ 109

Visualization

Tap to expand
Maximum Divisibility Score INPUT nums array: 4 7 9 3 9 [0] [1] [2] [3] [4] divisors array: 5 2 3 Goal: Find divisor with max divisibility score. Tie: return smallest divisor ALGORITHM STEPS 1 Check divisor 5 4%5!=0, 7%5!=0, 9%5!=0 3%5!=0, 9%5!=0 Score = 0 2 Check divisor 2 4%2=0 (OK) 7,9,3,9 not divisible Score = 1 3 Check divisor 3 9%3=0, 3%3=0, 9%3=0 3 nums divisible! Score = 3 4 Compare scores 5:0, 2:1, 3:3 Max score = 3 div=5: 0 | div=2: 1 | div=3: 3 Winner: divisor 3 (score=3) FINAL RESULT Divisibility Scores: 5: 0 2: 1 3: 3 (MAX) Output: 3 Divisor 3 divides nums[2], nums[3], nums[4] (values: 9, 3, 9) Key Insight: Single Pass Optimization: Iterate through each divisor once, counting how many nums elements are divisible by it. Track the maximum score and the smallest divisor achieving it. Time: O(n*m) where n=len(nums), m=len(divisors) | Space: O(1) TutorialsPoint - Find the Maximum Divisibility Score | Single Pass Optimization
Asked in
Google 25 Microsoft 20 Amazon 18
12.5K Views
Medium Frequency
~15 min Avg. Time
450 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