Minimum Deletions to Make Array Divisible
HardUpdated: Aug 2, 2025
Practice on:
Problem
You are given two positive integer arrays nums and numsDivide. You can delete any number of elements from nums.
Return _theminimum 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.
Examples
Example 1
Input: nums = [3,7,9], numsDivide = [3,9,6]
Output: 0
Explanation: 3 divides all elements in numsDivide. No deletions are needed.
Example 2
Input: nums = [4,3,6], numsDivide = [8,2,6,10]
Output: 2
Explanation: After deleting 4 and 6, nums = [3]. 3 does not divide 8, so return -1.
Example 3
Input: nums = [2,3,2,4,3], numsDivide = [9,6,9,3,15]
Output: 2
Explanation: After deleting two elements, nums = [3,3,3]. 3 divides all elements in numsDivide.
Constraints
1 <= nums.length, numsDivide.length <= 10^51 <= nums[i], numsDivide[i] <= 10^9
Solution
Method 1 – GCD and Sorting
Intuition
To make the smallest element in nums divide all elements in numsDivide, it must divide the GCD of numsDivide. We sort nums and try each element in increasing order, counting deletions until we find one that divides the GCD.
Approach
- Compute the GCD of all elements in
numsDivide. - Sort
numsin ascending order. - For each element in
nums, if it divides the GCD, return the number of deletions so far. - If no such element exists, return -1.
Code
C++
class Solution {
public:
int minDeletions(vector<int>& nums, vector<int>& numsDivide) {
int g = numsDivide[0];
for (int x : numsDivide) g = gcd(g, x);
sort(nums.begin(), nums.end());
for (int i = 0; i < nums.size(); ++i) {
if (g % nums[i] == 0) return i;
}
return -1;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
};
Go
func gcd(a, b int) int {
for b != 0 {
a, b = b, a%b
}
return a
}
func minDeletions(nums, numsDivide []int) int {
g := numsDivide[0]
for _, x := range numsDivide { g = gcd(g, x) }
sort.Ints(nums)
for i, x := range nums {
if g%x == 0 { return i }
}
return -1
}
Java
class Solution {
public int minDeletions(int[] nums, int[] numsDivide) {
int g = numsDivide[0];
for (int x : numsDivide) g = gcd(g, x);
Arrays.sort(nums);
for (int i = 0; i < nums.length; ++i) {
if (g % nums[i] == 0) return i;
}
return -1;
}
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
}
Kotlin
class Solution {
fun minDeletions(nums: IntArray, numsDivide: IntArray): Int {
fun gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
var g = numsDivide[0]
for (x in numsDivide) g = gcd(g, x)
nums.sort()
for ((i, x) in nums.withIndex()) {
if (g % x == 0) return i
}
return -1
}
}
Python
class Solution:
def minDeletions(self, nums: list[int], numsDivide: list[int]) -> int:
from math import gcd
from functools import reduce
g = reduce(gcd, numsDivide)
nums.sort()
for i, x in enumerate(nums):
if g % x == 0:
return i
return -1
Rust
impl Solution {
pub fn min_deletions(nums: Vec<i32>, nums_divide: Vec<i32>) -> i32 {
fn gcd(a: i32, b: i32) -> i32 {
if b == 0 { a } else { gcd(b, a % b) }
}
let mut g = nums_divide[0];
for &x in &nums_divide { g = gcd(g, x); }
let mut nums = nums;
nums.sort();
for (i, &x) in nums.iter().enumerate() {
if g % x == 0 { return i as i32; }
}
-1
}
}
TypeScript
class Solution {
minDeletions(nums: number[], numsDivide: number[]): number {
function gcd(a: number, b: number): number {
return b === 0 ? a : gcd(b, a % a
{{< /code_tabs >}}