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.
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 | Time | Space | Key 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. |