LeetCode Challenge Day 75 β 3512.Minimum Operations to Make Array Sum Divisible by K
Nitin Ahirwal / November 29, 2025
Hey folks π
This is Day 75 of my LeetCode streak π
Today's problem is Minimum Operations to Make Array Sum Divisible by K β one of the cleanest math-based problems in the series.
π Problem Statement
You are given an array nums and an integer k.
Your task: find the minimum number of operations needed to make the sum of the array divisible by k.
Each operation allows you to increment any element by 1.
π‘ Intuition
To make the total sum divisible by k, we only need to check:
remainder = sum(nums) % k
If remainder == 0, the sum is already divisible β 0 operations.
Otherwise, we need exactly remainder increments to reach the next multiple of k.
This works because every increment increases the total sum by 1.
π Approach
-
Compute the total sum of the array.
-
Return
sum % kβ the remainder directly represents the number of operations needed.
This solution is optimal because increments are the only allowed operation.
β±οΈ Complexity Analysis
ComplexityValue TimeO(n) SpaceO(1)
π§βπ» Code (JavaScript)
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minOperations = function(nums, k) {
const sum = nums.reduce((a, b) => a + b, 0);
return sum % k;
};
π― Reflection
This is a perfect example of how simple modulo arithmetic can turn a problem into a one-line solution.
-
β No complex logic
-
β Pure math
-
β Runs in linear time with constant space
That's it for Day 75 of my LeetCode challenge πͺ
See you tomorrow!
Happy Coding π¨βπ»