CodeLab / Problems / #453

453. Minimum Moves to Equal Array Elements

Medium
ArrayMath
Problem Statement
Given an integer array nums of size n, return the minimum number of moves required to make all array elements equal. In one move, you can increment n - 1 elements of the array by 1.
Example 1:
Input: nums = [1,2,3]   Output: 3
// [1,2,3] → [2,3,3] → [3,4,3] → [4,4,4]
Example 2:
Input: nums = [1,1,1]   Output: 0
My Initial Solution
Sort the array first so the minimum is at index 0, then sum all differences nums[i] - nums[0]. Correct, but sorting costs O(n log n) when we can do it in O(n) — the sort is unnecessary.
Strategy: Sort → Accumulate differences from minimum
Time: O(n log n)   Space: O(1)
High-Level Algorithm Steps
    Multi-Language Code & Execution Simulation
    Strategy:
    Compare Languages:
    Press play to start the simulation.
    Step 0 / 0
    Algorithm Performance & Complexity Chart
    SELECTED APPROACH
    My Answer
    TIME COMPLEXITY
    O(n log n)
    SPACE COMPLEXITY
    O(1)
    O(n log n) — My Answer (Sort) O(n) — Math Single-Pass O(n) — Relative Accumulation
    Comprehensive Strategy Matrix
    StrategyTimeSpaceKey Idea / Trade-offs
    My Answer — Sort & Sum O(n log n) O(1) Sort places min at index 0. Sum all diffs. Simple but sort is wasteful.
    AI 1 — Math Single-Pass O(n) O(1) Formula: sum − n × min. One loop finds both. Risk of int overflow on sum.
    AI 2 — Relative Accumulation O(n) O(1) Find min first, then sum (nums[i] − min). Overflow-safe; clean two-pass.