{x}
blog image

Minimum Operations to Make Array Equal

You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i (i.e., 0 <= i < n).

In one operation, you can select two indices x and y where 0 <= x, y < n and subtract 1 from arr[x] and add 1 to arr[y] (i.e., perform arr[x] -=1 and arr[y] += 1). The goal is to make all the elements of the array equal. It is guaranteed that all the elements of the array can be made equal using some operations.

Given an integer n, the length of the array, return the minimum number of operations needed to make all the elements of arr equal.

 

Example 1:

Input: n = 3
Output: 2
Explanation: 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].

Example 2:

Input: n = 6
Output: 9

 

Constraints:

  • 1 <= n <= 104

Solution Explanation: Minimum Operations to Make Array Equal

This problem asks for the minimum number of operations needed to make all elements in an array equal. The array arr is special: arr[i] = (2 * i) + 1. This means it's an arithmetic sequence starting at 1 and increasing by 2 with each element.

The key insight is that the sum of the array elements remains constant throughout the operations. We are only shifting values between elements, not changing the total sum.

Mathematical Approach:

  1. Sum of the Array: The sum of an arithmetic series is given by n/2 * (first_term + last_term). In our case:

    • first_term = 1
    • last_term = 2n - 1 (since arr[n-1] = 2(n-1) + 1)
    • Therefore, the sum is n/2 * (1 + 2n - 1) = n^2
  2. Target Value: If all elements are equal, each element must be n^2 / n = n.

  3. Operations: We need to figure out how many operations it takes to transform the array so that each element is equal to n. Let's consider the elements:

    • arr[0] = 1 needs n - 1 additions to become n
    • arr[1] = 3 needs n - 3 additions to become n
    • ...and so on. The last few elements will be subtracted from.
  4. Simplified Formula: Instead of summing up all the operations individually, we can use a simplified formula. The number of operations can be calculated as:

    sum(n - (2i + 1) for i in range(n // 2)) 
    

    This formula directly calculates the number of operations needed to balance the array to make all elements equal to n.

Time and Space Complexity:

  • Time Complexity: O(n). The loop iterates through approximately n/2 elements.
  • Space Complexity: O(1). We are only using a few variables to store the intermediate values. No additional data structures are needed.

Code Implementation (Python):

class Solution:
    def minOperations(self, n: int) -> int:
        return sum(n - (i << 1 | 1) for i in range(n >> 1))

This Python code efficiently calculates the sum using a generator expression and bitwise operations (<< and |) for faster calculations. n >> 1 is equivalent to n // 2 (integer division).

Code Implementation (Java):

class Solution {
    public int minOperations(int n) {
        int ans = 0;
        for (int i = 0; i < n >> 1; ++i) {
            ans += n - (i << 1 | 1);
        }
        return ans;
    }
}

The Java code is structurally similar to the Python code, utilizing bitwise operations for optimization.

Code Implementation (C++):

class Solution {
public:
    int minOperations(int n) {
        int ans = 0;
        for (int i = 0; i < n >> 1; ++i) {
            ans += n - (i << 1 | 1);
        }
        return ans;
    }
};

The C++ code mirrors the same logic and optimizations as Python and Java.

Other Languages (Go and TypeScript): The implementation in Go and TypeScript would follow the same algorithmic approach, with minor syntax changes to adapt to the specific language features. The core logic remains consistent across all languages.