Ctrl K

Raise All but One

Medium
You get an integer array values of length n. In one move you pick any n - 1 elements and add 1 to each of them. Return the minimum number of moves after which all elements are equal.
Example 1
values=[2, 5, 3] → 4

A move only changes the gap between the skipped number and the rest, so always skip a maximum: [2,5,3] → [3,5,4] → [4,5,5] → [5,6,5] → [6,6,6]. That is 4 moves = (2 + 5 + 3) - 3 · 2.

Example 2
values=[7, 7] → 0

The numbers are already equal.

Example 3
values=[-4] → 0
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
  • The answer is at most 2³¹ - 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTurn a Square Clockwise MediumPostfix Calculator MediumRoll an Array to the Right MediumPrimes Below a Bound MediumNth Number Built From Given Primes Medium
loading Python…
Input
values=[2, 5, 3]
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 76 tests on the server, hidden ones included.