Minimum Deletions to Make Array Divisible - Complete Solution Guide
Minimum Deletions to Make Array Divisible is LeetCode problem 2344, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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
Detailed Explanation
The problem asks us to find the minimum number of elements we need to delete from an array `nums` such that the smallest element remaining in `nums` divides all elements in another array `numsDivide`. If it's not possible to achieve this divisibility, we return -1. An integer 'x' divides 'y' if 'y % x == 0'. The goal is to minimize the number of deletions.
Solution Approach
The solution approach involves finding the GCD of all elements in `numsDivide`, sorting the `nums` array, and then iterating through the sorted `nums` array to find the smallest element that divides the GCD. The index of this element in the sorted array represents the minimum number of deletions required, as all elements before it must be deleted to make it the smallest.
Step-by-Step Algorithm
- Step 1: Calculate the Greatest Common Divisor (GCD) of all the numbers in `numsDivide`. The GCD represents the largest number that divides all the numbers in `numsDivide` without any remainder. This step is crucial because any number that divides all elements in `numsDivide` must also divide their GCD.
- Step 2: Sort the `nums` array in ascending order. Sorting allows us to iterate through the elements in increasing order and find the smallest element that could potentially divide the GCD.
- Step 3: Iterate through the sorted `nums` array. For each element, check if it divides the GCD (i.e., GCD % element == 0). If it does, return the index of that element, as it represents the minimum number of deletions needed to make it the smallest element.
- Step 4: If the loop completes without finding any element in `nums` that divides the GCD, it means no solution exists. Return -1 in this case.
Key Insights
- Insight 1: The smallest element in `nums` after deletions must divide every element in `numsDivide`. This is equivalent to the smallest element dividing the Greatest Common Divisor (GCD) of all elements in `numsDivide`.
- Insight 2: Sorting `nums` allows us to efficiently find the smallest element that satisfies the divisibility condition. We can then easily determine the number of elements smaller than it, which represent the minimum deletions required.
- Insight 3: If no element in `nums` divides the GCD of `numsDivide`, there is no solution and we return -1.
Complexity Analysis
Time Complexity: O(n log n + m)
Space Complexity: O(1)
Topics
This problem involves: Array, Math, Sorting, Heap (Priority Queue), Number Theory.
Companies
Asked at: LinkedIn.