commit 4d00acb36fc9112a26ffd875a27d78e07aa69c23
parent 5fe64bc7f2fa626f33a1ae2de96a7db8235b8dfd
Author: Jack Sangdahl <jack@pngu.org>
Date: Fri, 28 Nov 2025 21:18:05 -0700
3512. minimum operations to make array sum divisible by k
Diffstat:
2 files changed, 48 insertions(+), 0 deletions(-)
diff --git a/min_ops_arr_sum_div_k.c b/min_ops_arr_sum_div_k.c
@@ -0,0 +1,23 @@
+// 3512. Minimum Operations to Make Array Sum Divisible by K
+#include "leetcode.h"
+
+/*
+ * 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 to make
+ * the sum of the array divisible by 'k'.
+ */
+
+int minOperations(int *nums, int numsSize, int k) {
+ int sum = 0;
+ for (int i = 0; i < numsSize; i++)
+ sum += nums[i];
+ return sum % k;
+}
+
+int main() {
+ int n1[] = {3, 9, 7}, n2[] = {4, 1, 3}, n3[] = {3, 2};
+ printf("%d\n", minOperations(n1, ARRAY_SIZE(n1), 5)); // expect: 4
+ printf("%d\n", minOperations(n2, ARRAY_SIZE(n2), 4)); // expect: 0
+ printf("%d\n", minOperations(n3, ARRAY_SIZE(n3), 6)); // expect: 5
+}
diff --git a/min_ops_arr_sum_div_k.py b/min_ops_arr_sum_div_k.py
@@ -0,0 +1,25 @@
+# 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 to make
+the sum of the array divisible by 'k'.
+"""
+
+
+class Solution(object):
+ def minOperations(self, nums, k):
+ """
+ :type nums: List[int]
+ :type k: int
+ :rtype: int
+ """
+ return sum(nums) % k
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minOperations(nums=[3, 9, 7], k=5))
+ print(obj.minOperations(nums=[4, 1, 3], k=4))
+ print(obj.minOperations(nums=[3, 2], k=6))