Problem · Array

Minimum Operations to Make the Integer Zero

Learn this problem
MediumAmazon logoAmazonNEW GRADFULLTIMEOA
See Amazon hiring insights

Problem statement

In this problem, you are given an integer array and you need to perform some operations on the array to make all the elements equal to 0. In one operation, you can select a prefix of the given array and increment or decrement all the elements of the prefix by 1.

You have an array, arr, consisting of n integers. Find the minimum number of operations required to convert every element of this array to 0.

A prefix is a contiguous group of items that includes the first element in the cart. For example, [1], [1, 2], [1, 2, 3] etc are prefixes of [1, 2, 3, 4, 5].

Note: It is guaranteed that it is always possible to convert every element of the array to 0.

Function

getMinimumOperations(arr: int[]) → int

Complete the function getMinimumOperations in the editor below.

getMinimumOperations has the following parameter:

  • arr[n]: an array of integers

Returns

  • int: denoting the minimum number of operations required to convert every element to 0

Note: This problem has been intentionally rephrased due to unavoidable circumstances. The rephrased version preserves the exact meaning of the original prompt. You can view the original wording in the Problem Source section. Thank you for understanding! 🪿 (06-23-2026 :)

Update 06-26-2026: Get Minimum Operations is a duplicate of this problem. I merged its last seen dates into this page, and I recommend practicing this version because it is more complete. 🐘

Examples

Example 1

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

The most efficient approach is:

  • Operation 1: Let the prefix length be 2, and decrement by 1. cart after this operation is [2, 1, 1].
  • Operation 2: Let the prefix length be 1, and decrement by 1. cart after this operation is [1, 1, 1].
  • Operation 3: Let the prefix length be 3, and decrement by 1. cart after this operation is [0, 0, 0].

The answer is 3. Note that it is not possible make all the elements of the array 0 in fewer operations.

Example 2

arr = [3, 2, 0, 0, -1]return = 5

For n = 5 and cart = [3, 2, 0, 0, -1], one optimal set of operations is:

  • Operation 1: Let the prefix length be 1, and decrement by 1. cart after this operation is [2, 2, 0, 0, -1].
  • Operation 2: Let the prefix length be 2, and decrement by 1. cart after this operation is [1, 1, 0, 0, -1].
  • Operation 3: Let the prefix length be 4, and decrement by 1. cart after this operation is [0, 0, -1, -1, -1].
  • Operation 4: Let the prefix length be 2, and decrement by 1. cart after this operation is [-1, -1, -1, -1, -1].
  • Operation 5: Let the prefix length be 5, and increment by 1. cart after this operation is [0, 0, 0, 0, 0].

The answer is 5.

Constraints

  • 1 <= n <= 10^5
  • -10^9 <= cart[i] <= 10^9

More Amazon problems

drafts saved locally
public int getMinimumOperations(int[] arr) {
  // write your code here
}
arr[3, 2, 1]
expected3
checking account