Minimum Operations to Make Array Equal: Math Pattern
You are given an integer n. There is an array arr of length n where arr[i] = 2 * i + 1 (0-indexed). In one operation, you pick two indices and increment one element while decrementing the other. Return the minimum number of operations to make all elements equal.
This is LeetCode 1551: Minimum Operations to Make Array Equal, a medium problem that looks like it needs simulation but actually collapses into a single math formula. Once you see the pairing pattern, the answer is just n * n // 4.
Why this problem matters
This problem is a pure exercise in mathematical pattern recognition. The array is perfectly symmetric around its mean, and each operation transfers one unit between two elements. That symmetry means you can pair elements from opposite ends and compute the total cost in closed form.
The skill it drills, finding a formula by analyzing the structure of the input rather than simulating operations, appears in many math-tagged problems. If you can spot the pattern here, you will recognize similar shortcuts in problems involving arithmetic sequences, sums of differences, and parity arguments.
The key insight
The array [1, 3, 5, ..., 2n-1] is an arithmetic sequence of odd numbers. Its mean is n, and every element eventually needs to equal n. Because the array is symmetric around n, you can pair the smallest element with the largest, the second smallest with the second largest, and so on. Each pair needs exactly (larger - smaller) / 2 operations.
For the pair at distance k from the center, the cost is 2k - 1 operations. Summing these costs across all n/2 pairs gives:
For even n: 1 + 3 + 5 + ... + (n-1) which equals (n/2)^2 = n^2/4.
For odd n: 2 + 4 + ... + (n-1) also equals n^2/4 under integer division.
Both cases simplify to n * n // 4. That is the entire solution.
The solution
def minOperations(n):
return n * n // 4
The formula n * n // 4 handles both odd and even n correctly. For even n, n^2 is always divisible by 4, so integer division is exact. For odd n, n^2 leaves a remainder of 1 when divided by 4, and the floor gives the right answer because the middle element is already at the target and contributes zero cost.
You do not need to memorize n * n // 4 as a standalone trick. The derivation is what matters: pair elements symmetrically, compute the cost of each pair, and sum the series. If you forget the formula, you can re-derive it in under a minute by writing out the pairs for a small example.
Visual walkthrough
Let's trace through n = 6 step by step. The array is [1, 3, 5, 7, 9, 11] and the target value is 6.
Step 1: Identify the array and target
arr = [1, 3, 5, 7, 9, 11]. The target (mean of all elements) is n = 6.
Step 2: Pair (1, 11): need 5 operations
1 needs +5 to reach 6, and 11 needs -5 to reach 6. Each operation moves one unit, so 5 operations total.
Step 3: Pair (3, 9): need 3 operations
3 needs +3 to reach 6, and 9 needs -3 to reach 6. That is 3 operations.
Step 4: Pair (5, 7): need 1 operation
5 needs +1 to reach 6, and 7 needs -1 to reach 6. Just 1 operation.
Step 5: Total operations and the formula
The paired differences form the series (n-1), (n-3), (n-5), ... which sums to n*n/4.
The three pairs contribute 5, 3, and 1 operations respectively. The total is 9, which matches 6 * 6 // 4 = 36 // 4 = 9.
Complexity analysis
| Approach | Time | Space |
|---|---|---|
| Simulation (loop pairing) | O(n) | O(1) |
| Math formula | O(1) | O(1) |
The loop approach iterates through n/2 pairs and sums the differences. It works, but the formula makes it O(1). Both are accepted for this problem's constraints, but the O(1) solution demonstrates the deeper understanding that interviewers look for.
Building blocks
This problem is built on two reusable patterns that CodeBricks drills independently.
1. Arithmetic series sum
The costs form an arithmetic series: 1, 3, 5, ... for even n, or 2, 4, 6, ... for odd n. Recognizing that the sum of the first k odd numbers is k^2 (and the sum of the first k even numbers is k * (k + 1)) lets you collapse a loop into a formula. This building block appears whenever you need to sum a predictable sequence of differences.
2. Pairing from ends
Pairing the smallest with the largest, the second smallest with the second largest, and so on is a classic technique for symmetric arrays. Each pair has the same sum (here, 2n), and the distance from the target is symmetric. This pattern shows up in problems like Two Sum (sorted array version), minimum moves problems, and any scenario where you need to balance values toward a center.
Edge cases
n = 1. The array is [1] and the target is 1. No operations needed. 1 * 1 // 4 = 0. Correct.
n = 2. The array is [1, 3] and the target is 2. One operation: decrement 3 and increment 1. 2 * 2 // 4 = 1. Correct.
Large n. The formula handles any n up to 10^4 (the constraint) instantly. No overflow risk with standard 32-bit or 64-bit integers.
Odd n. When n is odd, the middle element is already at the target and does not need pairing. The formula n * n // 4 naturally accounts for this because integer division drops the remainder.
Even n. All elements pair up. No element starts at the target. The formula gives the exact sum of all pair costs.
From understanding to recall
The formula is simple, but that simplicity can be deceptive. Under interview pressure, you might second-guess whether it is n * n // 4 or n * (n - 1) // 4, or wonder whether you need special handling for odd versus even n. These doubts creep in when you have only read the solution, not practiced deriving it.
The path to confidence is re-deriving the formula from scratch: write out the array, identify the target, pair elements, compute costs, sum the series. Do that a few times at increasing intervals and the derivation becomes automatic. You will not need to remember the formula because you will reconstruct it faster than you can look it up.
Spaced repetition makes this concrete. You see the problem, you write the derivation, you get the formula. After three or four reps, the pairing-and-summing pattern is locked in. The next time a symmetric array or arithmetic series shows up, you reach for the same technique without thinking.
Related posts
- Minimum Moves to Equal Array Elements - Another "make all elements equal" problem where counting operations reduces to a formula
- Can Make Arithmetic Progression - Recognizing arithmetic sequences and their properties
- Count Odd Numbers in an Interval Range - A math-based counting problem where floor division replaces iteration
CodeBricks breaks the minimum operations problem into its arithmetic series and pairing building blocks, then drills them independently with spaced repetition. You type the derivation from scratch until the pattern is automatic. When a symmetric-array math question shows up in your interview, you do not hesitate. You write the formula and move on.