Skip to main content

Minimum Operations to Make Array Sum Divisible by K - Solution & Explanation

EasyArrayMath6 min readAsked at: Amazon, Microsoft, Meta +1
Practice this problem

Problem Statement

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.

 

Example 1:

Input: nums = [3,9,7], k = 5

Output: 4

Explanation:

  • Perform 4 operations on nums[1] = 9. Now, nums = [3, 5, 7].
  • The sum is 15, which is divisible by 5.

Example 2:

Input: nums = [4,1,3], k = 4

Output: 0

Explanation:

  • The sum is 8, which is already divisible by 4. Hence, no operations are needed.

Example 3:

Input: nums = [3,2], k = 6

Output: 5

Explanation:

  • Perform 3 operations on nums[0] = 3 and 2 operations on nums[1] = 2. Now, nums = [0, 0].
  • The sum is 0, which is divisible by 6.

 

Constraints:

  • 1 <= nums.length <= 1000
  • 1 <= nums[i] <= 1000
  • 1 <= k <= 100

Approach Overview

Problem 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.

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).

Code

Python

Java

C++

Go

TypeScript

Rust

C#

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen 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

Video Solution

Minimum Operations to Make Array Sum Divisible by K - Leetcode 3512 - Python • NeetCodeIO • 6,124 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Minimum Operations to Make Array Sum Divisible by K easy or hard?
Minimum Operations to Make Array Sum Divisible by K is classified as an Easy problem. The core idea relies on recognizing a modular arithmetic pattern after computing the array sum, making the implementation short and efficient.
Minimum Operations to Make Array Sum Divisible by K Python/Java solution
The implementation in Python, Java, C++, Go, TypeScript, Rust, or C# follows the same logic: compute the array sum, calculate sum % k, and return (k - remainder) % k. All implementations run in O(n) time and O(1) space.
How to solve Minimum Operations to Make Array Sum Divisible by K in O(n)?
Iterate through the array and compute the total sum. Calculate the remainder r = sum % k. If r is zero, no operations are needed. Otherwise return (k - r) % k, which represents the minimum increments required to reach the next multiple of k.
What is the best approach for Minimum Operations to Make Array Sum Divisible by K?
The optimal approach is the Sum and Modulo method. First compute the total array sum, then calculate the remainder with k using sum % k. The number of operations required is (k - remainder) % k. This runs in O(n) time for summing the array and uses O(1) extra space.
Is Minimum Operations to Make Array Sum Divisible by K asked at Google/Amazon/Meta?
This type of problem appears in coding interviews at large tech companies because it tests understanding of modular arithmetic and array aggregation. Variants involving divisibility, prefix sums, or modulo adjustments are commonly seen in Google, Amazon, and Meta interview question sets.
What data structure is used in Minimum Operations to Make Array Sum Divisible by K?
No specialized data structure is required. The solution uses a simple array traversal to compute the total sum and basic mathematical operations. The main concept comes from modular arithmetic rather than complex data structures.
What is the time complexity of Minimum Operations to Make Array Sum Divisible by K?
The optimal solution runs in O(n) time because you must iterate through the array once to compute the total sum. The modulo calculation and final formula are constant-time operations. Space complexity is O(1) since only a few integer variables are used.

Ready to solve this problem?

Practice Minimum Operations to Make Array Sum Divisible by K with our built-in code editor and test cases.

Practice on FleetCode