Problem · Array

Make Power Non-decreasing

Learn this problem
MediumAmazonNEW GRADINTERNOA
See Amazon hiring insights

Problem statement

AWS provides scalable systems. A set of n servers are used for horizontally scaling an application.

The goal is to have the computational power of the servers in non-decreasing order. To do so, you can increase the computational power of each server in any contiguous segment by x. Choose the values of x such that after the computational powers are in non-decreasing order, the sum of the x values is minimum.

Function

findMinimumSum(power: int[]) → long

Complete the function findMinimumSum in the editor below.

findMinimumSum has the following parameter:

  • int power[n]: the computational powers of n different servers

Source correction (June 28, 2026): I found another source image that shows the function name and parameter more clearly, so I fixed this version to match it. If you have the full prompt or see something off, please tell us and we'll fix it. If a clearer source shows up later, I'll update this again. 🐿️

Examples

Example 1

power = [3, 4, 1, 6, 2]return = 7

There are n = 5 servers and their computational power is [3, 4, 1, 6, 2].

Add 3 units to the subarray (2, 4) and 4 units to the subarray (4, 4). The final arrangement of the servers is [3, 4, 4, 9, 9].

The answer is 3 + 4 = 7.

Example 2

power = [3, 2, 1]return = 2

Add 1 unit to subarray (1, 2) and 1 unit to subarray (2, 2). The final arrangement of servers is [3, 3, 3].

🐰 Source note: I restored this official example on July 15, 2026. I'm sorry it was missing before and for the inconvenience.

Example 3

power = [3, 5, 2, 3]return = 3

Add 3 units to subarray (2, 3). The final arrangement of servers is [3, 5, 5, 6].

🐣 Source note: I restored this official example on July 15, 2026. I'm sorry it was missing before and for the inconvenience.

Constraints

  • 1 <= n <= 10^5 (where n is the length of power)
  • 1 <= power[i] <= 10^9

More Amazon problems

drafts saved locally
public long findMinimumSum(int[] power) {
  // write your code here
}
power[3, 4, 1, 6, 2]
expected7
checking account