#3512
Easy Algorithms Minimum operations to make array sum divisible by k
Array Math
92.4% acceptance
Feb 25, 2026
323
36
You are given an integer array nums and an integer k. You can perform the following operation any number of times:
Select an index i and replace nums[i] with nums[i] - 1.
Return the minimum number of operations required to make the sum of the array divisible by k.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
let sum: i32 = nums.iter().sum();
sum.rem_euclid(k)
}
}