Ctrl K

Equalizer Sliders to a Preset

Hard
An equalizer panel has a row of sliders whose current levels are stored in the array levels. You need to set them to the preset preset of the same length: slider i must end up at level preset[i]. In one move you may take any block of **consecutive** sliders and shift all of them by 1 together, either all up or all down. Return the smallest number of moves after which levels matches preset.
Example 1
levels=[2, 2, 5, 5], preset=[3, 4, 6, 5] → 2

Raise sliders 0–2 by one: [3, 3, 6, 5]. Then raise slider 1 once more: [3, 4, 6, 5]. Two moves in total.

Example 2
levels=[4, 4, 4], preset=[5, 3, 5] → 3

Sliders 0 and 2 must go up while slider 1 between them must go down, so no single block covers them: one move each, 3 in total.

Example 3
levels=[7, 7], preset=[7, 7] → 0

Everything already matches, so no moves are needed.

Constraints
  • 1 ≤ levels.length = preset.length ≤ 10⁵
  • 1 ≤ levels[i], preset[i] ≤ 10⁸
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsRaising by Ranges HardTree of Leaf Products MediumHopping Along the Stones MediumWater Between Walls HardBiggest Solid Panel Block Hard
loading Python…
Input
levels=[2, 2, 5, 5], preset=[3, 4, 6, 5]
Expected
2

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