← Back to challenges

Equalize a List

PythonHardalgorithmsarrayslogicloops

Instructions

Mubashir needs your help to equalize (make all list elements the same) a list.

Create a function that takes a list of integers lst and a constant c and returns minimum number of operations required to equalize the given list.

Rules

  • You can choose any integer x to equalize the given list.
  • Pick a contiguous sublist of length not greater than the given c

Examples

lst = [1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 1], c = 2
equalize(lst, c) ➞ 4
You have chosen x = 1 (change all values of the list to 1)

[1, 2, 2, 1, 2, 1, 2, 2, 2, 1, 1, 1]
[1, 1, 1, 1, 2, 1, 2, 2, 2, 1, 1, 1] -> Step 1
[1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1] -> Step 2
[1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1] -> Step 3
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] -> Step 4

Notes

N/A

python3
Loading editor…
⌘ ↡ to run
Walks through the solution with reasoning and edge cases.