1069. Equalise by Raising

Medium3.5 points estimatedArrayMathsBrainteaser3 hints

A bakery has a row of ovens, and nums[i] is the temperature setting of oven i (settings may be negative on the controller's scale). One adjustment lets you choose exactly one oven to leave alone and raises the setting of every other oven by 1. Return the minimum number of adjustments after which all ovens show the same setting.

The answer can be much larger than 32 bits, so return it as a 64-bit integer. Replaying the adjustments one by one would take forever; find the count directly in O(n) time and O(1) extra space.

Example 1

Input:
nums = [4,9,6]
Output:
7
Explanation:

The total of 19 minus 3 times the smallest setting 4 gives 7 adjustments.

Example 2

Input:
nums = [10,10,10]
Output:
0
Explanation:

All ovens already match, so no adjustment is needed.

Example 3

Input:
nums = [-3,0,5,-3]
Output:
11
Explanation:

The sum is -1 and four times the smallest value -3 is -12, so the difference 11 is the answer.

Constraints

1 ≤ nums.length ≤ 105

-109 ≤ nums[i] ≤ 109

The answer fits in a signed 64-bit integer.

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…