Problem · Array

Minimum Total Cost to Equalize an Array

Learn this problem
MediumAtlassian logoAtlassianINTERNFULLTIMEOA

Problem statement

An operation chooses a prefix and adds any integer x to every value in that prefix at cost |x|. Return the minimum total cost needed to make every array element equal.

Function

findMinimumCost(arr: int[]) → long

Examples

Example 1

arr = [1,2,1,5]return = 6

The neighboring differences require costs 1, 1, and 4, for a total of 6.

Constraints

  • 1 <= arr.length <= 200000
  • -10^9 <= arr[i] <= 10^9

More Atlassian problems

drafts saved locally
public long findMinimumCost(int[] arr) {
  // Write your code here.
}
arr[1,2,1,5]
expected6
checking account