You are given two arrays: nums and numsDivide. Your goal is to find the minimum number of deletions from nums such that the smallest remaining element in nums can divide all elements in numsDivide.
Key Insight: An integer x divides y if y % x == 0 (no remainder).
For example, if nums = [2,3,2,4,3] and numsDivide = [9,6,9,3,15], we need to delete elements from nums until the smallest element can divide all numbers in numsDivide. The number 3 divides all elements in numsDivide, so we delete elements to make 3 the smallest.
Return -1 if it's impossible to achieve this goal.
Input & Output
Visualization
Time & Space Complexity
n unique elements in nums, m elements in numsDivide, k elements to check divisibility
Space for storing unique elements and counting
Constraints
- 1 โค nums.length, numsDivide.length โค 105
- 1 โค nums[i], numsDivide[i] โค 109
- All elements in arrays are positive integers