Skip to content

Instantly share code, notes, and snippets.

@tatsuyax25
Created November 29, 2025 17:05
Show Gist options
  • Select an option

  • Save tatsuyax25/3527a0e4639f8c31dfa8a8ac12e2f708 to your computer and use it in GitHub Desktop.

Select an option

Save tatsuyax25/3527a0e4639f8c31dfa8a8ac12e2f708 to your computer and use it in GitHub Desktop.
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
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var minOperations = function(nums, k) {
// Step 1: Calculate the sum of the array
let sum = 0;
for (let num of nums) {
sum += num;
}
// Step 2: Find the remainder when divided by k
let remainder = sum % k;
// Step 3: If remainder is 0, sum is already divisible by k
// Otherwise, we need 'remainder' operations to fix it
return remainder;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment