mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
31 lines
1.4 KiB
HTML
31 lines
1.4 KiB
HTML
<p>You have an array <code>arr</code> of length <code>n</code> where <code>arr[i] = (2 * i) + 1</code> for all valid values of <code>i</code> (i.e., <code>0 <= i < n</code>).</p>
|
|
|
|
<p>In one operation, you can select two indices <code>x</code> and <code>y</code> where <code>0 <= x, y < n</code> and subtract <code>1</code> from <code>arr[x]</code> and add <code>1</code> to <code>arr[y]</code> (i.e., perform <code>arr[x] -=1 </code>and <code>arr[y] += 1</code>). The goal is to make all the elements of the array <strong>equal</strong>. It is <strong>guaranteed</strong> that all the elements of the array can be made equal using some operations.</p>
|
|
|
|
<p>Given an integer <code>n</code>, the length of the array, return <em>the minimum number of operations</em> needed to make all the elements of arr equal.</p>
|
|
|
|
<p> </p>
|
|
<p><strong class="example">Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 3
|
|
<strong>Output:</strong> 2
|
|
<strong>Explanation:</strong> arr = [1, 3, 5]
|
|
First operation choose x = 2 and y = 0, this leads arr to be [2, 3, 4]
|
|
In the second operation choose x = 2 and y = 0 again, thus arr = [3, 3, 3].
|
|
</pre>
|
|
|
|
<p><strong class="example">Example 2:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong> n = 6
|
|
<strong>Output:</strong> 9
|
|
</pre>
|
|
|
|
<p> </p>
|
|
<p><strong>Constraints:</strong></p>
|
|
|
|
<ul>
|
|
<li><code>1 <= n <= 10<sup>4</sup></code></li>
|
|
</ul>
|