Leetcode 2644: Find the Maximum Divisibility Score

grid47
grid47
Exploring patterns and algorithms
Feb 16, 2024 7 min read

You are given two integer arrays, ’nums’ and ‘divisors’. For each element in ‘divisors’, you need to calculate the divisibility score, which is the number of elements in ’nums’ divisible by that divisor. Your goal is to return the divisor with the highest divisibility score. If multiple divisors have the same score, return the smallest one.
Problem
Approach
Steps
Complexity
Input: The input consists of two arrays: 'nums', an array of integers, and 'divisors', another array of integers representing the divisors.
Example: nums = [12, 18, 24, 36], divisors = [3, 4, 6]
Constraints:
• 1 <= nums.length, divisors.length <= 1000
• 1 <= nums[i], divisors[i] <= 10^9
Output: Return the integer from 'divisors' with the highest divisibility score, or the smallest one if multiple divisors have the same score.
Example: Output: 6
Constraints:
• The output must be the divisor with the highest divisibility score.
Goal: Find the divisor with the maximum divisibility score from the 'divisors' array.
Steps:
• Step 1: Initialize a count array to track divisibility scores for each divisor.
• Step 2: Iterate through each number in 'nums' and check if it is divisible by each divisor in 'divisors'.
• Step 3: For each divisor, increment its count if the number is divisible by it.
• Step 4: After processing all numbers, find the divisor with the highest divisibility score, breaking ties by choosing the smallest divisor.
Goal: Ensure that the solution handles arrays of up to 1000 elements efficiently and performs the divisibility checks correctly for large numbers.
Steps:
• Time complexity should be O(m * n), where m is the size of the 'nums' array and n is the size of the 'divisors' array.
Assumptions:
• Each number in 'nums' can be divisible by one or more divisors.
Input: Input: nums = [12, 18, 24, 36], divisors = [3, 4, 6]
Explanation: The divisibility scores are: 3 -> 4 numbers divisible, 4 -> 3 numbers divisible, 6 -> 3 numbers divisible. The highest score is 4, and the smallest divisor with that score is 3.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus