You are given two positive integer arrays nums and numsDivide. You can delete any number of elements from nums.
Return the minimum number of deletions such that the smallest element in nums divides all the elements of numsDivide. If this is not possible, return -1.
Note that an integer x divides y if y % x == 0.
Example 1:
Input: nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15] Output: 2 Explanation: The smallest element in [2,3,2,4,3] is 2, which does not divide all the elements of numsDivide. We use 2 deletions to delete the elements in nums that are equal to 2 which makes nums = [3,4,3]. The smallest element in [3,4,3] is 3, which divides all the elements of numsDivide. It can be shown that 2 is the minimum number of deletions needed.
Example 2:
Input: nums = [4,3,6], numsDivide = [8,2,6,10] Output: -1 Explanation: We want the smallest element in nums to divide all the elements of numsDivide. There is no way to delete elements from nums to allow this.
Constraints:
1 <= nums.length, numsDivide.length <= 1051 <= nums[i], numsDivide[i] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the smallest number of items we need to remove from a collection so that all remaining items evenly divide a specific number. The brute force method involves trying every single possible combination of removals to see if we can achieve this goal.
Here's how the algorithm would work step-by-step:
def min_deletions_brute_force(numbers, divisible_list):
list_length = len(numbers)
min_deletions = list_length
for i in range(1 << list_length):
subset = []
deletions = 0
for j in range(list_length):
if (i >> j) & 1:
subset.append(numbers[j])
else:
deletions += 1
if not subset:
continue
is_divisible = True
for divisible_number in divisible_list:
is_divisible_by_subset = False
for subset_number in subset:
if divisible_number % subset_number == 0:
is_divisible_by_subset = True
break
if not is_divisible_by_subset:
is_divisible = False
break
# Check if the subset satisfies the divisibility condition.
if is_divisible:
min_deletions = min(min_deletions, deletions)
# Update the minimum deletions.
if min_deletions == list_length:
return -1
return min_deletionsTo minimize deletions, we need to find the smallest number that divides all numbers in the second group and then count how many numbers in the first group are bigger than that smallest number. The key is realizing that number is the Greatest Common Divisor (GCD) of all numbers in the second group.
Here's how the algorithm would work step-by-step:
def minimum_deletions(numbers, divisible_list):
def find_greatest_common_divisor(first_number, second_number):
while second_number:
first_number, second_number = second_number, first_number % second_number
return first_number
def calculate_gcd_of_list(number_list):
result = number_list[0]
for i in range(1, len(number_list)):
result = find_greatest_common_divisor(result, number_list[i])
return result
# Crucial step: determines divisibility target for the main list.
divisible_gcd = calculate_gcd_of_list(divisible_list)
numbers.sort()
deletion_count = 0
# Iterating to find the smallest element that divides the GCD.
for number in numbers:
if divisible_gcd % number == 0:
# First divisor found.
return deletion_count
deletion_count += 1
return deletion_count| Case | How to Handle |
|---|---|
| nums is null or empty | Return -1 since no elements can be selected for divisibility. |
| numsDivide is null or empty | Return 0, as any smallest element from nums trivially divides an empty set. |
| nums contains negative numbers | The GCD or smallest element might be negative, so take the absolute value to ensure proper divisibility checks. |
| numsDivide contains negative numbers | Take the absolute value of numbers in numsDivide for divisibility checks. |
| nums contains zero | If the smallest element in nums is zero, return -1 as zero cannot divide any number. |
| numsDivide contains zero | The GCD of numsDivide will be zero; a valid smallest element from nums must be able to divide zero without issue, so find smallest element that works, or return -1 if none do. |
| No element in nums divides all elements in numsDivide | Return -1 because no solution is possible if no element in nums fulfills the divisibility condition. |
| nums contains duplicate values with varying frequencies | Count the occurrences of the smallest element(s) and handle deletions based on the smallest number and its frequency. |