Fewest Increment-All-but-One Moves to Make Array Values Equal
Company: Squarepoint
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: From a Squarepoint technical screen: one move adds 1 to every element of an integer array except one chosen element, and you must find the fewest moves that make all elements equal. It tests reasoning about what each move really changes, along with negative values and answers beyond the 32-bit range.
Read the full Squarepoint Data Scientist interview experience this question came from
Constraints
- 1 <= len(nums) <= 10^5
- -10^9 <= nums[i] <= 10^9
- The answer can reach about 2 * 10^14, which exceeds the 32-bit signed range but stays below 2^53.
Examples
Input: ([7],)
Expected Output: 0
Explanation: Minimum valid: a single-element array is already equal and needs 0 moves.
Input: ([1, 3],)
Expected Output: 2
Explanation: Source Example 1: keep 3 unchanged twice.
Hints
- Look at what a single move does to the gap between any two elements, not to their absolute values.
- Simulating the moves one at a time is far too slow when the answer can reach about 2 * 10^14.
- The answer can exceed the 32-bit signed range, so accumulate it in a 64-bit integer.