Ctrl K

Meet at One Value

Medium
You get an integer array values. In one step you may increase or decrease any element by 1. Return the minimum number of steps after which all elements are equal.
Example 1
values=[4, 1, 7] → 6

Bring everything to 4: raise 1 by 3 and lower 7 by 3, 6 steps in total.

Example 2
values=[0, 10, 2, 9] → 17

Any target from 2 to 9 costs the same; for 5 it is 5 + 5 + 3 + 4 = 17.

Example 3
values=[-3, -3] → 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 problemsSorted Parabola Values MediumChain of Multiples MediumClosest Clock Readings MediumUp or Down for Every Post MediumSmallest Paddock Medium
loading Python…
Input
values=[4, 1, 7]
Expected
6

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