|
| 1 | +<p>You are given an integer array <code>nums</code> and an integer <code>k</code>.</p> |
| 2 | + |
| 3 | +<p>In one operation, you can <strong>increase</strong> or <strong>decrease</strong> any element of <code>nums</code> by 1.</p> |
| 4 | + |
| 5 | +<p>An array is called <strong>modulo alternating</strong> if there exist two <strong>distinct</strong> integers <code>x</code> and <code>y</code> (<code>0 <= x, y < k</code>) such that:</p> |
| 6 | + |
| 7 | +<ul> |
| 8 | + <li>For every <strong>even</strong> index <code>i</code>, <code>nums[i] % k == x</code></li> |
| 9 | + <li>For every <strong>odd</strong> index <code>i</code>, <code>nums[i] % k == y</code></li> |
| 10 | +</ul> |
| 11 | + |
| 12 | +<p>Return the <strong>minimum</strong> number of operations required to make <code>nums</code> <strong>modulo alternating</strong>.</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | +<p><strong class="example">Example 1:</strong></p> |
| 16 | + |
| 17 | +<div class="example-block"> |
| 18 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,4,2,8], k = 3</span></p> |
| 19 | + |
| 20 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 21 | + |
| 22 | +<p><strong>Explanation:</strong></p> |
| 23 | + |
| 24 | +<ul> |
| 25 | + <li>Let's choose <code>x = 1</code> for even indices and <code>y = 2</code> for odd indices.</li> |
| 26 | + <li>Perform the following operations: |
| 27 | + <ul> |
| 28 | + <li>Increment <code>nums[1] = 4</code> by 1, giving <code>nums = [1, 5, 2, 8]</code>.</li> |
| 29 | + <li>Decrement <code>nums[2] = 2</code> by 1, giving <code>nums = [1, 5, 1, 8]</code>.</li> |
| 30 | + </ul> |
| 31 | + </li> |
| 32 | + <li>Now, for even indices, <code>nums[i] % k = 1</code>, and for odd indices, <code>nums[i] % k = 2</code>.</li> |
| 33 | + <li>Thus, the total number of operations required is 2.</li> |
| 34 | +</ul> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p><strong class="example">Example 2:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block"> |
| 40 | +<p><strong>Input:</strong> <span class="example-io">nums = [1,1,1], k = 3</span></p> |
| 41 | + |
| 42 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 43 | + |
| 44 | +<p><strong>Explanation:</strong></p> |
| 45 | + |
| 46 | +<ul> |
| 47 | + <li>Incrementing <code>nums[1]</code> by 1 gives <code>nums = [1, 2, 1]</code>, which satisfies the condition with <code>x = 1</code> and <code>y = 2</code>.</li> |
| 48 | + <li>Thus, the total number of operations required is 1.</li> |
| 49 | +</ul> |
| 50 | +</div> |
| 51 | + |
| 52 | +<p> </p> |
| 53 | +<p><strong>Constraints:</strong></p> |
| 54 | + |
| 55 | +<ul> |
| 56 | + <li><code>1 <= nums.length <= 100</code></li> |
| 57 | + <li><code>1 <= nums[i] <= 10<sup>9</sup></code></li> |
| 58 | + <li><code>2 <= k <= 100</code></li> |
| 59 | +</ul> |
0 commit comments