Ctrl K

Level It With Prefix Shifts

Easy
You get an integer array values. In one operation you choose any leading part of the array (its first few elements, at least one) and add the same integer k to each of them; k may be negative. Return the fewest operations needed to make all elements of the array equal.
Example 1
values=[5, 2, 2, 8] → 2

Add 6 to the first three: [11, 8, 8, 8]. Then add −3 to the first one: [8, 8, 8, 8]. Two operations.

Example 2
values=[-4, -4] → 0

Everything is already equal, so no operations are needed.

Example 3
values=[1, 3, 1, 3] → 3
Constraints
  • 1 ≤ len(values) ≤ 10⁵
  • -10⁹ ≤ values[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPair With a Target Sum EasySqueeze Repeats Out of a Sorted Array EasyDrop the Unwanted Value EasySlot on a Sorted Shelf EasyAdd One to a Digit Array Easy
loading Python…
Input
values=[5, 2, 2, 8]
Expected
2

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