3512. Minimum Operations to Make Array Sum Divisible by K Easy

@problem@discussion
#Array#Math



1/**
2 * [3512] Minimum Operations to Make Array Sum Divisible by K
3 *
4 * You are given an integer array nums and an integer k. You can perform the following operation any number of times:
5 * 
6 * 	Select an index i and replace nums[i] with nums[i] - 1.
7 * 
8 * Return the minimum number of operations required to make the sum of the array divisible by k.
9 *  
10 * <strong class="example">Example 1:
11 * <div class="example-block">
12 * Input: <span class="example-io">nums = [3,9,7], k = 5</span>
13 * Output: <span class="example-io">4</span>
14 * Explanation:
15 * 
16 * 	Perform 4 operations on nums[1] = 9. Now, nums = [3, 5, 7].
17 * 	The sum is 15, which is divisible by 5.
18 * </div>
19 * <strong class="example">Example 2:
20 * <div class="example-block">
21 * Input: <span class="example-io">nums = [4,1,3], k = 4</span>
22 * Output: <span class="example-io">0</span>
23 * Explanation:
24 * 
25 * 	The sum is 8, which is already divisible by 4. Hence, no operations are needed.
26 * </div>
27 * <strong class="example">Example 3:
28 * <div class="example-block">
29 * Input: <span class="example-io">nums = [3,2], k = 6</span>
30 * Output: <span class="example-io">5</span>
31 * Explanation:
32 * 
33 * 	Perform 3 operations on nums[0] = 3 and 2 operations on nums[1] = 2. Now, nums = [0, 0].
34 * 	The sum is 0, which is divisible by 6.
35 * </div>
36 *  
37 * Constraints:
38 * 
39 * 	1 <= nums.length <= 1000
40 * 	1 <= nums[i] <= 1000
41 * 	1 <= k <= 100
42 * 
43 */
44pub struct Solution {}
45
46// problem: https://leetcode.com/problems/minimum-operations-to-make-array-sum-divisible-by-k/
47// discuss: https://leetcode.com/problems/minimum-operations-to-make-array-sum-divisible-by-k/discuss/?currentPage=1&orderBy=most_votes&query=
48
49// submission codes start here
50
51impl Solution {
52    pub fn min_operations(nums: Vec<i32>, k: i32) -> i32 {
53        0
54    }
55}
56
57// submission codes end
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_3512() {
65    }
66}
67

Back
© 2026 bowen.ge All Rights Reserved.