You are given an integer array nums and an integer k. You can perform the following operation any number of times:
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.
Example 1:
Input: nums = [3,9,7], k = 5
Output: 4
Explanation:
nums[1] = 9. Now, nums = [3, 5, 7].Example 2:
Input: nums = [4,1,3], k = 4
Output: 0
Explanation:
Example 3:
Input: nums = [3,2], k = 6
Output: 5
Explanation:
nums[0] = 3 and 2 operations on nums[1] = 2. Now, nums = [0, 0].
Constraints:
1 <= nums.length <= 10001 <= nums[i] <= 10001 <= k <= 100Problem Overview: You are given an integer array and a number k. Each operation increases the total array sum by 1. The goal is to determine the minimum number of operations required so the final sum becomes divisible by k.
Approach 1: Increment Simulation (Brute Force) (Time: O(n + k), Space: O(1))
Start by computing the total array sum using a single pass through the array. If the sum is already divisible by k, the answer is 0. Otherwise, repeatedly simulate operations by incrementing the sum until sum % k == 0. Each increment represents one operation. In the worst case, you may need up to k-1 increments before the remainder reaches zero. This approach is easy to reason about and mirrors the problem statement directly, but the repeated increments are unnecessary once you realize the pattern in modular arithmetic. The array traversal is O(n), and the simulation loop adds up to O(k) steps.
Approach 2: Sum and Modulo (Optimal) (Time: O(n), Space: O(1))
The key observation comes from modular arithmetic. Let S be the array sum. If S % k = r, then the sum needs k - r additional increments to reach the next multiple of k. If the remainder is already zero, no operations are needed. This leads directly to the formula (k - (S % k)) % k. The extra modulo ensures the result becomes zero when the sum is already divisible. Implementation is straightforward: iterate through the array once to compute the sum, calculate the remainder with k, and return the required difference. The algorithm runs in linear time with constant space and avoids unnecessary iteration.
This technique relies on simple arithmetic properties and is common in problems involving divisibility and modular adjustments. Many array problems reduce to computing aggregates like sums or counts before applying a mathematical rule. Understanding how remainders behave under addition makes this pattern easy to reuse across similar math and array problems.
Recommended for interviews: The Sum and Modulo approach is what interviewers expect. Mentioning the brute force simulation first demonstrates understanding of the problem mechanics, but deriving the direct formula using modular arithmetic shows stronger problem-solving skills and leads to the optimal O(n) time and O(1) space solution.
The problem essentially asks for the result of the sum of the array elements modulo k. Therefore, we only need to iterate through the array, calculate the sum of all elements, and then take the modulo k. Finally, return this result.
The time complexity is O(n), where n is the length of the array nums. The space complexity is O(1).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Increment Simulation (Brute Force) | O(n + k) | O(1) | When first reasoning about the problem or explaining the mechanics of adding operations step by step |
| Sum and Modulo (Optimal) | O(n) | O(1) | General case and interview solution using modular arithmetic |
Minimum Operations to Make Array Sum Divisible by K - Leetcode 3512 - Python • NeetCodeIO • 5,253 views views
Watch 9 more video solutions →Practice Minimum Operations to Make Array Sum Divisible by K with our built-in code editor and test cases.
Practice on FleetCode