Skip to main content
Back to problems
#1551
Medium Algorithms

Minimum operations to make array equal

Math
82.7% acceptance
Feb 25, 2026
1502
187
You have an array arr of length n where arr[i] = (2 * i) + 1 for all valid values of i. 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]. The goal is to make all the elements of the array equal. Given an integer n, return the minimum number of operations needed to make all the elements of arr equal.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn min_operations(n: i32) -> i32 {
    // arr[i] = 2i+1, target = n, ans = n*n/4
    n * n / 4
  }
}