|
| 1 | +<h2><a href="https://leetcode.com/problems/minimum-operations-to-make-array-sum-divisible-by-k/?envType=daily-question&envId=2025-11-29">3846. Minimum Operations to Make Array Sum Divisible by K</a></h2><h3>Easy</h3><hr><p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You can perform the following operation any number of times:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li>Select an index <code>i</code> and replace <code>nums[i]</code> with <code>nums[i] - 1</code>.</li> |
| 5 | +</ul> |
| 6 | + |
| 7 | +<p>Return the <strong>minimum</strong> number of operations required to make the sum of the array divisible by <code>k</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong class="example">Example 1:</strong></p> |
| 11 | + |
| 12 | +<div class="example-block"> |
| 13 | +<p><strong>Input:</strong> <span class="example-io">nums = [3,9,7], k = 5</span></p> |
| 14 | + |
| 15 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 16 | + |
| 17 | +<p><strong>Explanation:</strong></p> |
| 18 | + |
| 19 | +<ul> |
| 20 | + <li>Perform 4 operations on <code>nums[1] = 9</code>. Now, <code>nums = [3, 5, 7]</code>.</li> |
| 21 | + <li>The sum is 15, which is divisible by 5.</li> |
| 22 | +</ul> |
| 23 | +</div> |
| 24 | + |
| 25 | +<p><strong class="example">Example 2:</strong></p> |
| 26 | + |
| 27 | +<div class="example-block"> |
| 28 | +<p><strong>Input:</strong> <span class="example-io">nums = [4,1,3], k = 4</span></p> |
| 29 | + |
| 30 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 31 | + |
| 32 | +<p><strong>Explanation:</strong></p> |
| 33 | + |
| 34 | +<ul> |
| 35 | + <li>The sum is 8, which is already divisible by 4. Hence, no operations are needed.</li> |
| 36 | +</ul> |
| 37 | +</div> |
| 38 | + |
| 39 | +<p><strong class="example">Example 3:</strong></p> |
| 40 | + |
| 41 | +<div class="example-block"> |
| 42 | +<p><strong>Input:</strong> <span class="example-io">nums = [3,2], k = 6</span></p> |
| 43 | + |
| 44 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 45 | + |
| 46 | +<p><strong>Explanation:</strong></p> |
| 47 | + |
| 48 | +<ul> |
| 49 | + <li>Perform 3 operations on <code>nums[0] = 3</code> and 2 operations on <code>nums[1] = 2</code>. Now, <code>nums = [0, 0]</code>.</li> |
| 50 | + <li>The sum is 0, which is divisible by 6.</li> |
| 51 | +</ul> |
| 52 | +</div> |
| 53 | + |
| 54 | +<p> </p> |
| 55 | +<p><strong>Constraints:</strong></p> |
| 56 | + |
| 57 | +<ul> |
| 58 | + <li><code>1 <= nums.length <= 1000</code></li> |
| 59 | + <li><code>1 <= nums[i] <= 1000</code></li> |
| 60 | + <li><code>1 <= k <= 100</code></li> |
| 61 | +</ul> |
0 commit comments