Created
November 29, 2025 17:05
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * @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