Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code.

Problem: What is the minimum number of operations to make all elements of [4, 7, 1] equal to their minimum by decrementing? Run the code.
a = [4, 7, 1]
m = min(a)
print(sum(x - m for x in a))
What is printed?
  1. A. 12
  2. B. 6
  3. C. 9
  4. D. 2

Correct answer: C. 9

Costs are (4 − 1) + (7 − 1) + 0 = 9. Decrementing to 0 gives 12, using only max − min gives 6, and 2 counts the elements that change.

More Coding Interview Problems questions

Exams that ask this topic