FastPrepMinimum Total Cost
Problems/Minimum Total Cost

Minimum Total Cost

JP Morgan Chase logoJP Morgan Chase● MediumINTERNOA

Updated Oct 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The prefix-adjustment task is supported by the source image. Numeric practice limits are authored because that image provides none: at most 1000 signed 32-bit values and a minimum cost within the signed 32-bit result range. A later internship report names Min Total Cost and a loop-based math approach but omits its rules; its association here is a practice interpretation, not a verified exact historical match.

Given an array of integers, the goal is to make all the elements in the array have equal values by applying some number of operations.

The rules of the operations are:

  • To apply an operation, one needs to choose a prefix of the array and an integer x (x can be negative).
  • In this operation, add x to each element inside this prefix.
  • The cost of this operation is |x| (Absolute value of x).

For example, if the array is [1, 4, 2, 1] and the prefix of length 2 and x = -3 are chosen, the array would now become [-2, 1, 2, 1] and the total cost of this operation would be |x| = |-3| = 3.

The total cost is the sum of costs of each operation applied. Find the minimum total cost of making all the elements in the array have equal value.

Note: It is guaranteed that there always exists a series of operations by which all the elements in any array can be equal. These operations can be applied any number of times.

Function

findMinimumCost(arr: List<Integer>) → int

Complete the function findMinimumCost in the editor below.

findMinimumCost has the following parameter:

  • List<Integer> arr: an array of integers

Examples

Example 1

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

The array can be made equal using the following three operations:

  1. Choose the prefix of length 2 and x = -1. Hence the array now becomes [0, 1, 1, 5]. The cost of this operation is |x| = |-1| = 1.
  2. Choose the prefix of length 3 and x = 4. Hence the array now becomes [4, 5, 5, 5]. The cost of this operation is |x| = |4| = 4.
  3. Choose the prefix of length 1 and x = 1. Hence the array now becomes [5, 5, 5, 5]. The cost of this operation is |x| = |1| = 1.

Thus, the total cost = 1 + 4 + 1 = 6, which is the minimum possible.

Constraints

  • 1 <= arr.length <= 1000
  • -2^31 <= arr[i] <= 2^31 - 1
  • The minimum total cost is at most 2^31 - 1.

More JP Morgan Chase interview questions

  • Longest Consecutive SequencePHONE SCREEN · Seen Nov 2026
  • LFU CacheONSITE INTERVIEW · Seen Nov 2026
  • LRU CacheONSITE INTERVIEW · Seen Nov 2026
  • Find Total CostOA · Seen Oct 2026
  • Minimum Meeting RoomsOA · Seen Oct 2026
  • Count Dropped RequestsOA · Seen Oct 2026
  • Maximize Binary XOR with a Set-Bit LimitPHONE SCREEN · Seen Oct 2026
  • Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
  • Task SchedulingOA · Seen Oct 2026
  • Substring RemovalOA · Seen Oct 2026

Similar questions at other companies

  • Minimum Operations to Sort with Rotations Then SwapsBank of New York Mellon · OA
  • Minimum Cost of Left and Right PropagationAmazon · OA
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Reschedule Meetings for Maximum Free TimeFivetran · OAFivetran interview questions
  • Maximum Easy Tasks After RearrangingIBM · OAIBM interview questions
  • Minimum Cost K-Capable ModelsMicrosoft · OAMicrosoft interview questions
  • Maximum ScoreNVIDIA · OANVIDIA interview questions
  • Maximum Distinct Values After Unit AdjustmentsBirla Pivot · OA
  • Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
  • Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions

Interview reports that mention this question

  • JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)

JP Morgan Chase interview experiences

  • JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
  • JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
  • JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
  • JP Morgan Chase Data and AI Intern Superday: 11 Experiences
  • JPMorgan Code for Good India: 10 Experiences (Part 4 of 5)
All JP Morgan Chase interview questionsSee JP Morgan Chase hiring insights
Saved
class Solution {
    public static int findMinimumCost(List<Integer> arr) {
      // Write your code here
    }

}
Checking account…