Skip to content

2644. Find the Maximum Divisibility Score 👍

  • Time: $O(|\texttt{nums}||\texttt{divisors}|)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
 public:
  int maxDivScore(vector<int>& nums, vector<int>& divisors) {
    int ans = -1;
    int maxScore = -1;

    for (const int divisor : divisors) {
      const int score = ranges::count_if(
          nums, [divisor](const int num) { return num % divisor == 0; });
      if (score > maxScore) {
        ans = divisor;
        maxScore = score;
      } else if (score == maxScore) {
        ans = min(ans, divisor);
      }
    }

    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class Solution {
  public int maxDivScore(int[] nums, int[] divisors) {
    int ans = -1;
    int maxScore = -1;

    for (final int divisor : divisors) {
      final int score = (int) Arrays.stream(nums).filter(num -> num % divisor == 0).count();
      if (score > maxScore) {
        ans = divisor;
        maxScore = score;
      } else if (score == maxScore) {
        ans = Math.min(ans, divisor);
      }
    }

    return ans;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Solution:
  def maxDivScore(self, nums: list[int], divisors: list[int]) -> int:
    ans = -1
    maxScore = -1

    for divisor in divisors:
      score = sum([1 for num in nums if num % divisor == 0])
      if score > maxScore:
        ans = divisor
        maxScore = score
      elif score == maxScore:
        ans = min(ans, divisor)

    return ans