[LeetCode] Minimum Operations to Make Array Sum Divisible by K

3512. Minimum Operations to Make Array Sum Divisible by K

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.

1
2
3
4
5
6
7
8
class Solution {
public:
int minOperations(vector<int>& nums, int k) {
int res = 0;
for(auto& n : nums) res = (res + n) % k;
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/04/13/PS/LeetCode/minimum-operations-to-make-array-sum-divisible-by-k/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.