Back to posts

LeetCode Challenge Day 75 β€” 3512.Minimum Operations to Make Array Sum Divisible by K

Nitin Ahirwal / November 29, 2025

LeetCode ChallengeDay 75MathModulo ArithmeticJavaScriptEasy

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

  1. Compute the total sum of the array.

  2. 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 πŸ‘¨β€πŸ’»