java.lang.Object
g1501_1600.s1551_minimum_operations_to_make_array_equal.Solution

public class Solution extends Object
1551 - Minimum Operations to Make Array Equal.<p>Medium</p> <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><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 3</p> <p><strong>Output:</strong> 2</p> <p><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].</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 6</p> <p><strong>Output:</strong> 9</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= n <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • minOperations

      public int minOperations(int n)