Minimum Operations to Make the Integer Zero
Learn this problemProblem 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[]) → intComplete 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 to0
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 = 3The most efficient approach is:
- Operation 1: Let the prefix length be
2, and decrement by1. cart after this operation is[2, 1, 1]. - Operation 2: Let the prefix length be
1, and decrement by1. cart after this operation is[1, 1, 1]. - Operation 3: Let the prefix length be
3, and decrement by1. 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 = 5For 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 by1. cart after this operation is[2, 2, 0, 0, -1]. - Operation 2: Let the prefix length be
2, and decrement by1. cart after this operation is[1, 1, 0, 0, -1]. - Operation 3: Let the prefix length be
4, and decrement by1. cart after this operation is[0, 0, -1, -1, -1]. - Operation 4: Let the prefix length be
2, and decrement by1. cart after this operation is[-1, -1, -1, -1, -1]. - Operation 5: Let the prefix length be
5, and increment by1. 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
- Drone Delivery RouteOA · Seen Aug 2026
- Package Dependency OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Unfulfilled Customers by Inventory PriorityOA · Seen Aug 2026
- Calculate Beauty ValuesOA · Seen Aug 2026
- Maximize Distance to the Closest Occupied SeatONSITE INTERVIEW · Seen Aug 2026
- Package Delivery SystemOA · Seen Aug 2026
- Select Least Resource TasksOA · Seen Aug 2026
- Maximum Length-K Window Sum over Sparse SegmentsOA · Seen Aug 2026